Hey there folks. I'm in the middle of learning C# using the book 'C# and Game programming: A Beginner's Guide' by Salvatore A Buono and I've come to a bit of a problem.
The example code for nested if and else-if statements does not work as intended. I could move on and just hope that later code works, but I'd prefer to get this working otherwise I won't really have a solid foundation to work from. The program runs, but after the 2nd user input (the y or n) the program seems to skip over all the options, print the output of the else statement regardless, and then even if I add a Console.ReadLine() it terminates. I'm rather confused to be honest.

Apologies for posting a chunk of code, but I couldn't think of any other way to explain it other than just showing it and sang "can you see anything specifically wrong with this?"

The code goes as follows (apologies for any formatting errors from posting, obviously everything that is meant to be on the same line should be):

/* Complex or nested if and if-else statements */
using System;

namespace Chapter2
{
    class Class1
    {
        static void Main()
        {
            string Input;
            float Age, Weight;
            int iQuickAnswer;

            // note: you'll want to hit return before entering the second answer        
            Console.Write("How old are you and how much do you weigh? ");
            Input = Console.ReadLine();
            Age = float.Parse(Input);
            Input = Console.ReadLine();
            Weight = float.Parse(Input);

            if (Age < 3 || Weight < 35)
            { // start compound if statement     
                Console.Write("\nThe law requires you to sit in a car seat\n"
                    + "\n Do you have a car seat? ");
                iQuickAnswer = Console.Read();

                if (iQuickAnswer == 'y') // nested if statement
                    Console.Write("\n Good, but using it would be better.\n");
                else
                { // nested else compounded    
                    Console.Write("\n No, do you care about your baby?\n");
                    if (iQuickAnswer == 'y') // nested if in nested else
                        Console.Write("\n Well then get a car seat!\n");
                    else if (iQuickAnswer == 'n') // nested else-if in nested else
                        Console.Write("\n You sicken me!\n"
                            + "\n Your baby is worth it!\n");
                    else // nested else in nested else
                        Console.Write("\n Your baby is worth it!\n");
                }
            } // end compound if statement
            else if (Age > 85 || Weight > 500)
                Console.WriteLine("\n Sorry I asked!\n");
        }
    }
}

Edit: If it helps, I'm using Microsoft Visual C# 2008 Express Edition (I figure it's a good place to start as I hope to move on to programming games using XNA Game Studio Express once I have the fundamentals down.

Please use code tags when posting code on daniweb:

[code]

...code here...

[/code]

The code path depends on the inputs you are providing. Please provide the age/weight you are using.

Hey, thanks for replying. Sorry about the formatting, have tried to go back and edit it but I can't seem to find the edit button. I had it earlier but it seems to have vanished!

I generally just enter an arbitrary integer for either input. Last few tries I just typed 12 for both. What I find odd is that the code seems to expect multiple inputs from the user but doesn't actually provide the opportunity to allow said input.

Crazy thing is, I wrote a slightly different program from scratch using nested if and else-if statements later this evening and it worked fine, but I would still really like to know what is wrong with this code before I move on. I guess it'd be good to know what mistakes to look out for in the rest of the provided code, especially as I'm completely baffled as to what the problem is.

iQuickAnswer should be a char. it can technically be an int, but that might mess up your comparisons.
also, if you are testing for 'y' you should probably test for 'Y' also (iQuickAnswer == 'y' || iQuickAnswer == 'Y'). the same for 'n' and 'N'.

your syntax is laid out correctly, but to reassure yourself about your nesting you could put all the optional braces in to delineate your "if/else if/else" groupings.
There may be other errors, but that's what I got from a quick glance.

Console.Write("\n No, do you care about your baby?\n");
if (iQuickAnswer == 'y') // nested if in nested else

You arent checking for the users input here. You need to include another iQuickAnswer = Console.Read(); to capture what they enter. Otherwise, it will use the same answer to the first if for the second one :)

Remember to mark the thread as solved if you have found an answer to your question.

Hey folks,

thanks for the advice, I've got the program working fine now. Once I'd converted iQuickAnswer (now cQuickAnswer) into a char, my compiler refused to accept console.Read(), instead only accepting console.ReadLine(). Why is this? I thought console.Read() was just for any single number or character? Also, until I changed that, any instance of if (cQuickAnswer == 'y') gave the error 'cannot implicitly convert char to bool'.

But either way, this one is solved, but will post the code below anyway.

using System;

namespace Chapter2
{
    class Class1
    {
        static void Main()
        {
            string Input;
            float Age, Weight;
            char cQuickAnswer;

            // note: you'll want to hit return before entering the second answer        
            Console.Write("How old are you and how much do you weigh? ");
            Input = Console.ReadLine();
            Age = float.Parse(Input);
            Input = Console.ReadLine();
            Weight = float.Parse(Input);

            if (Age < 3 || Weight < 35)
            { // start compound if statement     
                Console.Write("\nThe law requires you to sit in a car seat\n"
                    + "\n Do you have a car seat? ");
                Input = Console.ReadLine();
                cQuickAnswer = char.Parse(Input);
                
                if (cQuickAnswer == 'y' || cQuickAnswer == 'Y') // nested if statement
                    Console.Write("\n Good, but using it would be better.\n");
                else
                { // nested else compounded    
                    Console.Write("\n No, do you care about your baby?\n");
                    Input = Console.ReadLine();
                    cQuickAnswer = char.Parse(Input);
                    if (cQuickAnswer == 'y'||cQuickAnswer == 'Y') // nested if in nested else
                        Console.Write("\n Well then get a car seat!\n");
                    else if (cQuickAnswer == 'n') // nested else-if in nested else
                        Console.Write("\n You sicken me!\n"
                            + "\n Your baby is worth it!\n");
                    else // nested else in nested else
                        Console.Write("\n Your baby is worth it!\n");
                }
            } // end compound if statement
            else if (Age > 85 || Weight > 500)
                Console.WriteLine("\n Sorry I asked!\n");

Console.ReadLine();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.