6 Replies - 504 Views - Last Post: 18 February 2013 - 08:01 AM Rate Topic: -----

#1 wolverineandrew  Icon User is offline

  • New D.I.C Head

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

Built if statement wrong.

Posted 17 February 2013 - 07:31 PM

My goal with this was to take the input and save the age. Based on the age with the first if statements make classif equal child teen adult or senior. After that with the second if statements depending on what classif equaled in the previous statement set the rate to either 1.0, 1.5, 1.7 or 2.0 as i declared above. The program works with no bug but it does not print the intended information. When i put the age of 23 in my classification comes out as 0 and my rate as 1. The classification was supposed to be either child, teen, adult or senior, yet a number comes out. And the rate also is incorrect as well, it should be a 1.7 not a 1. Im assuming i set this up wrong, can any one follow this so far and help me out??

 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();
        }
    }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Built if statement wrong.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,075
  • Joined: 02-June 10

Re: Built if statement wrong.

Posted 17 February 2013 - 07:51 PM

Breakpoints. I know it was said in your other thread that you need to put in breakpoint and debug your code looking at the runtime values of the variables. That's still true. You need to put a breakpoint at line 16, then walk through the execution line by line looking at the run-time values of the variables to see *why* things happen.

As soon as I did that I saw my age of 45 be set to childRate.

Posted Image

So then I ask: Why? Because two lines above classif must equal child.
So then I ask: What is the value of child?
Oh look, child, teen, adult and senior are ALL set to the same value of "0".

So when my age of 45 was entered, classif was set to the adult - but adult, teen and so on are all the same.

So yeah, the computer is doing exactly what you told it to do.
Was This Post Helpful? 1
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Built if statement wrong.

Posted 17 February 2013 - 07:52 PM

string classif, child = "0", teen = "0", adult = "0", senior = "0";



You've set the values for all of those as "0", so when you set classif equal to any of them it's going to be "0" as well.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,075
  • Joined: 02-June 10

Re: Built if statement wrong.

Posted 17 February 2013 - 07:55 PM

Posted Image

This wasn't a great mystery or even tough. You just had to debug and not ask others to do it for you because you don't want to take the time to learn. Do the tutorials.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#5 wolverineandrew  Icon User is offline

  • New D.I.C Head

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

Re: Built if statement wrong.

Posted 17 February 2013 - 08:12 PM

You know what, i completely agree with you. Its frustrating actually. Debug has only been covered to the point of click debug. I dont even have the work breaking point in my c# vocabulary yet. The quality of learning for this course is not high at all, which sucks for some one who actually wants to learn it and not just put a program together for an assignment. Thats the first thing i will do after i finish this assignment, learn to properly debug.

(That window you showed where it showed the values of my variables, how is that visible?)

So i set them to 0 because i thought you had to do that with things you have declared that you might not use (another bad teaching moment from my professor i assume?). Taking the = 0 out of the 4 of them and putting "" around each child teen adult and senior in the If statements i assume will fix this. Please help me learn to fish, i have your debugging tutorial open and will study it tonight. Do you have any other advice for me?
Was This Post Helpful? 0
  • +
  • -

#6 wolverineandrew  Icon User is offline

  • New D.I.C Head

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

Re: Built if statement wrong.

Posted 17 February 2013 - 08:35 PM

More specificly, switch and enum were the most recent poorly taught subjects that didnt come across all that well, if you could point me in a good direction for learning that would be great. Thank you
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,075
  • Joined: 02-June 10

Re: Built if statement wrong.

Posted 18 February 2013 - 08:01 AM

There are numerous books out there. Buy a couple. There is no law that says you have to deliberately keep yourself in the dark and only go by the course guide. Get a couple "Learn C# in 30 days" books. Buy a good book just on Visual Studio 2012.

If you stick with the course only you will be the same as everyone else. If you put in some extra effort and look for alternative resources it will be easier to ace the class.

Quote

(That window you showed where it showed the values of my variables, how is that visible?)

When you run your app in debug (F5) Locals and Autos should appear as tabs next to Output. If not, open from
Debug | Windows | Locals
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1