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 


My C++ code
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Apr 11, 2009 10:15 am    Post subject: Reply with quote

switch case might be more readable in this case
Back to top
View user's profile Send private message
talkerzero
Grandmaster Cheater
Reputation: 1

Joined: 24 Jul 2008
Posts: 560
Location: California

PostPosted: Sat Apr 11, 2009 10:39 am    Post subject: Reply with quote

And, it's a good idea to always try to avoid using goto.
Back to top
View user's profile Send private message Visit poster's website
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sat Apr 11, 2009 10:42 am    Post subject: Reply with quote

http://cplusplus.com/doc/tutorial/control/
Back to top
View user's profile Send private message
VolatileAce
Cheater
Reputation: 0

Joined: 19 Mar 2009
Posts: 30
Location: 0001001100110111

PostPosted: Sat Apr 11, 2009 11:54 am    Post subject: Reply with quote

mydogjax wrote:
Ok, so if how do i make it loop? Whats the syntax for it?


Say I want to simply print out all even numbers from 0 up to 20.

You could, do this:
Code:
int i = 0;
cout << i << endl;
cout << (i+=2) << endl; //2
cout << (i+=2) << endl; //4
cout << (i+=2) << endl; //6
cout << (i+=2) << endl; //8
cout << (i+=2) << endl; //10
cout << (i+=2) << endl; //12
cout << (i+=2) << endl; //14
cout << (i+=2) << endl; //16
cout << (i+=2) << endl; //18
cout << (i+=2) << endl; //20


However, as you can see, repeated code is extremely ugly (and waste of space), therefore we resort to other approaches.

While using goto is usually discouraged, sometimes it could be useful. (goto require you to make up some sort of name for that place to "go to", in this case the I named it "loop")
Code:
loop:
    if(i <= 20){
        cout << i << endl;
        i += 2;
        goto loop;
    }


If you do it with a while loop, it would look something like this:
Code:
int i = 0;
while(i <= 20){
    cout << i << endl;
    i += 2;
}


There is a variation of the while loop call the do-while loop, this type of while loop is designed so that it will always at least execute once.
Here's a way to do it with the do-while loop.
Notice unlike other loops, there is a semicolon following the while.
Code:
int i = 0;
do{
    cout << i << endl;
    i += 2;
}while(i <= 20);

which could be simplified into:
Code:
int i = 0;
do{
    cout << i << endl;
}while((i+=2) <= 20);


And last, the for loop, which is basically a "extra featured while loop".
The for loop takes in 3 "parameter", the "initial statement", "condition", and "post statement", I'll explain this after I show you the example:
Code:
for(int i = 0; i <= 20; i+=2){
    cout << i << endl;
}

Technically you don't have to put a variable declaration in the first statement, in fact, you can even leave it blank. (However, the for loop is mainly used as a counter loop so if you are not going to use these you might as well just stick with a while loop. Also notice the first statement of a for loop will be executed only once) The middle is the condition, its what you would put as if its the condition you put in a while loop. Last is the post statement, this statement is executed every time the loop is going to be restarted again.

Hope this helps, ask if you have further questions.
Back to top
View user's profile Send private message
Black Ghost
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2008
Posts: 68

PostPosted: Sat Apr 11, 2009 1:07 pm    Post subject: Reply with quote

This might be nub Q...

What compiler did u all using ?
Back to top
View user's profile Send private message MSN Messenger
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sat Apr 11, 2009 1:24 pm    Post subject: Reply with quote

Black Ghost wrote:
This might be nub Q...

What compiler did u all using ?


http://www.microsoft.com/express/vc/
or you can *cough cough* download (torrent/warez) the full Microsoft Visual Studios
Back to top
View user's profile Send private message
Black Ghost
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2008
Posts: 68

PostPosted: Sat Apr 11, 2009 1:42 pm    Post subject: Reply with quote

@manc

aren't that for c++ .NET?
Back to top
View user's profile Send private message MSN Messenger
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sat Apr 11, 2009 1:48 pm    Post subject: Reply with quote

Sorry I don't know much about the whole .NET schemes.
I have used both the compiler in the link I provided for regular c++ compiling, as well as the full version. They both work fine with standard c/c++ code and don't have anything to do with .NET (as far as I know).
Back to top
View user's profile Send private message
Black Ghost
Advanced Cheater
Reputation: 0

Joined: 05 Aug 2008
Posts: 68

PostPosted: Sat Apr 11, 2009 2:03 pm    Post subject: Reply with quote

k ty =].
rep+ for being helpful and nice.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Apr 11, 2009 2:34 pm    Post subject: Reply with quote

Black Ghost wrote:
@manc

aren't that for c++ .NET?


You need to also download the Windows Platform SDK for native C/C++
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
Goto page Previous  1, 2
Page 2 of 2

 
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