Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Basic If Else Question

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shadoninja
Master Cheater
Reputation: 0

Joined: 10 Aug 2007
Posts: 376

PostPosted: Wed Jun 25, 2008 1:34 pm    Post subject: Basic If Else Question Reply with quote

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
View user's profile Send private message
avril18
Master Cheater
Reputation: 0

Joined: 11 Apr 2007
Posts: 380
Location: En san salvador, El Salvador

PostPosted: Wed Jun 25, 2008 2:04 pm    Post subject: Reply with quote

is bracket problem
try this:

Code:
 if (expression)

statement1;

else

statement2;

next statement;


_________________


Back to top
View user's profile Send private message
shadoninja
Master Cheater
Reputation: 0

Joined: 10 Aug 2007
Posts: 376

PostPosted: Wed Jun 25, 2008 2:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 25, 2008 2:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
shadoninja
Master Cheater
Reputation: 0

Joined: 10 Aug 2007
Posts: 376

PostPosted: Wed Jun 25, 2008 2:34 pm    Post subject: Reply with quote

Awesome. thank you
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Wed Jun 25, 2008 2:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 25, 2008 5:40 pm    Post subject: Reply with quote

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites