| View previous topic :: View next topic |
| Author |
Message |
shadoninja Master Cheater
Reputation: 0
Joined: 10 Aug 2007 Posts: 376
|
Posted: Wed Jun 25, 2008 1:34 pm Post subject: Basic If Else Question |
|
|
This is an if else statement that I wrote out of practice to make sure I could do if else but apparently I can't...... Can someone tell me what I did wrong here? Dev-C++ stops at else
| Code: | {
if (nPassword == 69)
cout << "Name: Joey\n\n";
cout << "Address: Here\n\n";
else
cout << "GTFO NEWB!!!\n\n";
}
|
Last edited by shadoninja on Wed Jun 25, 2008 2:19 pm; edited 1 time in total |
|
| Back to top |
|
 |
avril18 Master Cheater
Reputation: 0
Joined: 11 Apr 2007 Posts: 380 Location: En san salvador, El Salvador
|
Posted: Wed Jun 25, 2008 2:04 pm Post subject: |
|
|
is bracket problem
try this:
| Code: | if (expression)
statement1;
else
statement2;
next statement;
|
_________________
|
|
| Back to top |
|
 |
shadoninja Master Cheater
Reputation: 0
Joined: 10 Aug 2007 Posts: 376
|
Posted: Wed Jun 25, 2008 2:23 pm Post subject: |
|
|
| avril18 wrote: | is bracket problem
|
hmm well I changed it to this
| Code: |
if (nPassword == 69)
cout << "Name: Joey\n\n";
cout << "Address: Here\n\n";
else
cout << "GTFO NEWB!!!\n\n";
|
And it still didn't work (saying it expected a semi-colon before "else")
but I changed the two strings just to repeat the integer back to you like this
| Code: |
if (nPassword == 69)
cout << nPassword;
else
cout << "GTFO NEWB!!!\n\n";
|
and both scenarios worked fine. Do you know why a string would create a semi-colon error like that?
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 25, 2008 2:27 pm Post subject: |
|
|
if your if/while/for/etc loops are more than one line of code, you need to surround the block in brackets {}.
| Code: |
if(condition)
statement1;
else
statement2; |
is perfectly accpetable, but
| Code: |
if(condition)
statement1;
satement2;
else
statement3;
|
is not. You'd need to change it to
| Code: |
if(condition){
statement1;
satement2;}
else
statement3;
|
to work. If you don't use braces, the compiler won't know how long the block is. It assumes the block is only 1 statement. If you need more than 1 statement, like in your example, you'll need to use the brackets to tell the compiler how big the block is.
If you also needed more than 1 statement in "else", you'd need to sue brackets, too.
_________________
|
|
| Back to top |
|
 |
shadoninja Master Cheater
Reputation: 0
Joined: 10 Aug 2007 Posts: 376
|
Posted: Wed Jun 25, 2008 2:34 pm Post subject: |
|
|
| Awesome. thank you
|
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Wed Jun 25, 2008 2:46 pm Post subject: |
|
|
| Just forget that. Always use the {} when using if, for ... etc those loops. That will ensure that you do not screw your code up.
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 25, 2008 5:40 pm Post subject: |
|
|
| dnsi0 wrote: | | Just forget that. Always use the {} when using if, for ... etc those loops. That will ensure that you do not screw your code up. |
Uh, no.
It's usually considered standard practice, and (in my opinion) looks nicer if you don't include the { } with one-liners.
_________________
|
|
| Back to top |
|
 |
|