Help me out with this...

This is a discussion on Help me out with this... within the C++ Programming forums, part of the General Programming Boards category; Ok am so green to C++ it's scary! So please bear with me. I keep getting this error when I ...

Thanks Ninja! It worked. The tutorial from the site instructed me to have { and } brackets for the code I want executed for the 'if' and 'else' statements. Guess it was wrong.

What if I wanted to add code after the else statement that has nothing do with that if/else statement? Wouldn't the code assume that the code I add is part of the else statement? How do I end it? Is is just an 'End if' or something like that?

The copiler takes onely one sttement after an if/else, anything after that, it just goes on normal with. as for the braces ({}), the tutorial was right about that, It's better code for when the program will be updated in the future so the updater don't have to put them in.

I see what you mean. I changed it back to use the brackets.....it helps me see and read the code alot clearer.

Thanks guys you have been a great help!

Originally posted by Salem > Guess it was wrong.
No, it was quite correct

The problem started here
if(varNumber==8);
See that ; at the end?

This is equivalent to

Code:

if(varNumber==8) {
// do nothing
} else {
// do nothing
}

Since you can add a new pair of {} in your code pretty much whenever you like

Code:

{
cout << "Right" <<endl;
}

had nothing at all to do with the if statement, and you would have seen "Right" output all the time regardless.

By the time the compiler got to the 'else', all trace of processing an 'if' was gone, and the 'else' became a totally unexpected thing to see in the code.
Thus, the error message
parse error before `else'

When dealing with small programs it's easy to keep your code close together (and very neat). I've sorted your code out a bit, take note. For single line code inside if statments (such as a breif cout screen print) it is easier (also neater) to include your code on the same line.

>When dealing with small programs it's easy to keep your code close together (and very neat).
Compact isn't always neat, and even small programs should try to be readable. I'll very rarely place the body of an if statement on the same line as the condition because people are conditioned to look at the condition and then move to the next line for the body. I like my code to be readable so that you can run through it and not have to stop here and there to figure out what's going on: