Declaring wrong?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 652 Views - Last Post: 17 February 2013 - 10:31 AM Rate Topic: -----

#1 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Declaring wrong?

Posted 16 February 2013 - 10:29 PM

What am i doing wrong here with my classif? It says it isnt declared when i debut it but its clearly declared. How did i declare it wrong?

 namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            int age, childD = 12, teenD = 18, adultD = 65, seniorD = 66;
            string classif, child = "0", teen = "0", adult = "0", senior = "0";
            //Ask their age
            Console.WriteLine("How old are you?");
            //Save the answer
            age = int.Parse(Console.ReadLine());
            //Display persons age
            Console.WriteLine("Persons Age: {0}", age);
            //Calculate the class and multiplier
            if (age <= childD)
            {
                classif = child;
                
            }
            else if (age <= teenD && age > childD)
            {
                classif = teen;
            }
            else if (age <= adultD && age > teenD)
            {
                classif = adult;
            }
            else if (age >= seniorD)
            {
                classif = senior;
            }
            //Display classification
            Console.WriteLine("Classification: {0}", classif);
            //Stay on page
            Console.ReadLine();
        }
    }
} 

This post has been edited by GunnerInc: 17 February 2013 - 12:12 AM
Reason for edit:: Fixed broken code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Declaring wrong?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 16 February 2013 - 11:14 PM

What do you mean by "when I debut it"? On which line do you get the error and what is the exact error message? Does it really say that you didn't declare the variable or does it say that you're using (possibly) uninitialized?
Was This Post Helpful? 0
  • +
  • -

#3 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 16 February 2013 - 11:16 PM

Its the "Console.WriteLine("Classification: {0}", classif);" classif that isnt working by the way. Also, i know i did the [/code] wrong. I couldnt find how to edit my past post, i apologize.

Debug, not debut haha sorry, its late.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 16 February 2013 - 11:27 PM

Please post the exact error message you're getting. I find it really hard to believe that it says that the variable is not declared.
Was This Post Helpful? 0
  • +
  • -

#5 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 16 February 2013 - 11:40 PM

It says use of unassigned local variable 'classif'
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 16 February 2013 - 11:54 PM

That's the error message you get when there's a path of execution in which your variable does not get assigned a value before it is used. In this case that path happens if none of your if- and else-if-conditions are true. Of course that can't actually happen because those conditions cover every case, but the compiler doesn't know that. The only way the compiler can know that one of the branches of an if-else-if construct will definitely execute is if it ends with an else.

So if you replace your last else if with an else, it should work fine.
Was This Post Helpful? 0
  • +
  • -

#7 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 12:04 AM

And then how do i end it? When i change that else (age >= seniorD) is now an error saying ";expected" but if i put a ";" after the "else (age >= seniorD)" its an error as well.

oh wait, i dont need anything there huh. I just need "else". Duh
Was This Post Helpful? 0
  • +
  • -

#8 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 12:17 AM

Ok, last help. I added a bit at the end, starting around line 35 ish. im doing something wrong, it says i cant convert the strings to bools. How do i alter what i did to correctly code this?
 namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            int age, childD = 12, teenD = 18, adultD = 65, seniorD = 66;
            string classif, rateMul, child = "0", teen = "0", adult = "0", senior = "0";
            double childRate = 1.0, teenRate = 1.5, adultRate = 1.7, seniorRate = 2.0;
            //Ask their age
            Console.WriteLine("How old are you?");
            //Save the answer
            age = int.Parse(Console.ReadLine());
            //Display persons age
            Console.WriteLine("Persons Age: {0}", age);
            //Calculate the class
            if (age <= childD)
            {
                classif = child;
            }
            else if (age <= teenD && age > childD)
            {
                classif = teen;
            }
            else if (age <= adultD && age > teenD)
            {
                classif = adult;
            }
            else
            {
                classif = senior;
            }
            //Calculate multiplier
            if (classif = child)
            {
                rateMul = childRate;
            }
            else if (classif = teen)
            {
                rateMul = teenRate;
            }
            else if (classif = adult)
            {
                rateMul = adultRate;
            }
            else (classif = senior);
            {
                rateMul = seniorRate;
            }
            //Display classification
            Console.WriteLine("Classification: {0}", classif);
            Console.WriteLine("Rate Multiplier: {0}", rateMul);
            //Stay on page
            Console.ReadLine();
        }
    }
} 

Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 12:40 AM

= is the assignment operator. It evaluates to the value being assigned. So if (classif = child) is basically a shortcut for:

classif = child;
if(classif)



Since classif is a string that makes no sense and you get the error message you do.

Obviously what you intended to check whether classif was equal to child. The equality operator is ==.
Was This Post Helpful? 0
  • +
  • -

#10 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 12:52 AM

would i use that on both parts of the second if equations? Or just the top parts? the (classif -- child) parts? Im obviously pretty new to this coding, and apparently i dont have an eye for detail yet.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 01:14 AM

You should use == when you want to check whether two things are equal. You should use = when you want to change the value of a variable to something else.
Was This Post Helpful? 0
  • +
  • -

#12 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 01:59 AM

Ok, so it looks like ive set it up wrong. You have helped me fix everything to this point, and thank you very much. so I have it bug free but when i run it and put in 23 as the age rateMul displays a 1 and classif displays a 0. now my intentions were for rateMul to display one of the numbers i have declared (1, 1.5, 1.7. 2) and for classif to display child teen adult or senior.
 namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            int age, childD = 12, teenD = 18, adultD = 65, seniorD = 66;
            string classif, child = "0", teen = "0", adult = "0", senior = "0";
            double rateMul, childRate = 1.0, teenRate = 1.5, adultRate = 1.7, seniorRate = 2.0;
            //Ask their age
            Console.WriteLine("How old are you?");
            //Save the answer
            age = int.Parse(Console.ReadLine());
            //Display persons age
            Console.WriteLine("Persons Age: {0}", age);
            //Calculate the class
            if (age <= childD)
            {
                classif = child;
            }
            else if (age <= teenD && age > childD)
            {
                classif = teen;
            }
            else if (age <= adultD && age > teenD)
            {
                classif = adult;
            }
            else
            {
                classif = senior;
            }
            //Calculate multiplier
            if (classif == child)
            {
                rateMul = childRate;
            }
            else if (classif == teen)
            {
                rateMul = teenRate;
            }
            else if (classif == adult)
            {
                rateMul = adultRate;
            }
            else
            {
                rateMul = seniorRate;
            }
            //Display classification
            Console.WriteLine("Classification: {0}", classif);
            Console.WriteLine("Rate Multiplier: {0}", rateMul);
            //Stay on page
            Console.ReadLine();
        }
    }
} 

Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 03:05 AM

It will always go in the classif == child case because both classif and child are always "0" (and so are all your other string variables, but that doesn't matter because it doesn't check the else-ifs because the first already matched).
Was This Post Helpful? 0
  • +
  • -

#14 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 08:47 AM

I dont fully understand how to correct my code with what that last message said....
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 08:55 AM

Do you fully understand why your current code does not work as you want it to?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2