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 


[Help]C++ Loops
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
MegaForum
Grandmaster Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 558

PostPosted: Thu Jul 03, 2008 12:41 pm    Post subject: [Help]C++ Loops Reply with quote

Edit: Thanks to HalfPrime, lurc, Flyte, noz3001 for helping the most so far Smile. I've been following your ideas and advice and am close to finishing... I just have 1 error. Can help me fix it, ty Smile

fatal error C1083: Cannot open include file: 'ctime.h': No such file or directory

Here's my new code as well. I might of screwed something up XD.

Code:

#include "iostream.h"
#include "stdlib.h"
#include "ctime.h"
using namespace std;
int main()
{
   int number1;
   int number2;
   int number3;
   int tries;
   int mynumber = rand() % 100 + 1;
   
   cout << "You get 3 tries to match my number! 1-99"<< endl;   
   for(int tries=1; tries <=3; tries++)
   {
      cout << "Enter your number" << tries << endl;
      cin >> number;
      if(number == mynumber)
      {
         cout << "You got my number correct!" << endl;
         break;
      }
      else if(number != mynumber)
      {
         cout << "Your nubmer is incorrect!" << endl;
      }
   }
   system("PAUSE");
   return 0;
}



Thanks for the help. Razz


Last edited by MegaForum on Thu Jul 03, 2008 9:13 pm; edited 1 time in total
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Thu Jul 03, 2008 12:52 pm    Post subject: Reply with quote

Make an array of integer's and then create a for loop.

Array:
int nNums[3];

For loop:
for ( initialize variables ; condition ; inc/dec/sleep/etc )

Then just do

cin >> nNums[i]; // if i was the number u initialized in your for loop.

_________________
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jul 03, 2008 1:01 pm    Post subject: Reply with quote

<iostream>
<stdlib.h>

use this instead of system("PAUSE")

cin.sync()
cin.ignore()

It's a fine idea, go for it, maybe rework it a bit though so it's a bit more fun and fair heh

Create an array of some random numbers.

Then just have it loop until the person gets the number. If they get it wrong, have it tell them if it's higher or lower, if they get it right, increment. Once they get so many right, stop.

Just use a do while loop.
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: Thu Jul 03, 2008 1:08 pm    Post subject: Reply with quote

add "#include <windows.h>" to give you access to the clock() function. This is one of the functions used to generate random numbers. It returns how long the program has been open for.
the mod operator "%" returns the remainder of a division between 2 numbers. ie 13%4 would return 1 as 13 is 1 more than 12(4*3). You can use this to get a number from 1-99 from clock(). mynumber=(clock()%99)+1.

If you look at your source, you repeat that block of code 3 times. This is an excellent place for a loop. a for loop is just a while loop that does some extra stuff automatically for you.
Code:
for(int tries=1;tries <=3;tries++){
cout <<"Enter number" << tries << endl;
cin >> number;
if(number == mynumber)
{
cout <<"You got my number correct!"<< endl;
break;
}
else if(number != mynumber)
{
cout <<"Your number is incorrect!"<< endl;
}
}

The for loop will repeat from 1 to 3 taking in a number and comparing it to the random number. if they're equal, it'll say you win and "break;". This means it will break out of the for loop and go down to the system("PAUSE") and return. if they're not equal, it will say that and start the loop again.

Once you understand this and want to improve, try seeing if you can tell them whether the number is higher or lower than what they put in.

_________________
Back to top
View user's profile Send private message
MegaForum
Grandmaster Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 558

PostPosted: Thu Jul 03, 2008 1:08 pm    Post subject: Reply with quote

lurc wrote:
Make an array of integer's and then create a for loop.

Array:
int nNums[3];

For loop:
for ( initialize variables ; condition ; inc/dec/sleep/etc )

Then just do

cin >> nNums[i]; // if i was the number u initialized in your for loop.


Can you explain the array a little better =x. I tried it but I get a warning saying " The variable 'nNums' is being used without being initialized."
Also to slovach : i use iostream.h because that's how its named in visual C++ 2008 to show its a header file. It uses iostream, but its labeld iostream.h Razz
Back to top
View user's profile Send private message
Chaosis13
Master Cheater
Reputation: 0

Joined: 14 Aug 2007
Posts: 372

PostPosted: Thu Jul 03, 2008 1:09 pm    Post subject: Reply with quote

Well... You shouldn't do a loop, unless it becomes a bigger, then a for loop.
Code:
for(int x = 0; x = 3, x++){
//stuff
}


And at end use var x and an if statement to say "You did it in " + x + " trys!" or "You couldn't guess it! Fuck you! It was 5!".

And you should make the number random each time.

Just some tips...
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Thu Jul 03, 2008 1:11 pm    Post subject: Reply with quote

MegaForum wrote:
lurc wrote:
Make an array of integer's and then create a for loop.

Array:
int nNums[3];

For loop:
for ( initialize variables ; condition ; inc/dec/sleep/etc )

Then just do

cin >> nNums[i]; // if i was the number u initialized in your for loop.


Can you explain the array a little better =x. I tried it but I get a warning saying " The variable 'nNums' is being used without being initialized."
Also to slovach : i use iostream.h because that's how its named in visual C++ 2008 to show its a header file. It uses iostream, but its labeld iostream.h Razz


If you wanna store 3 different numbers then you'll have to use an array.
If you just wanna check for the number, read HalfPrime's code.
Arrays are just simply multiple variables all built into a big table.
you can access each by placing an index between the square brackets [ ]

_________________
Back to top
View user's profile Send private message
MegaForum
Grandmaster Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 558

PostPosted: Thu Jul 03, 2008 1:13 pm    Post subject: Reply with quote

HalfPrime wrote:
add "#include <windows.h>" to give you access to the clock() function. This is one of the functions used to generate random numbers. It returns how long the program has been open for.
the mod operator "%" returns the remainder of a division between 2 numbers. ie 13%4 would return 1 as 13 is 1 more than 12(4*3). You can use this to get a number from 1-99 from clock(). mynumber=(clock()%99)+1.

If you look at your source, you repeat that block of code 3 times. This is an excellent place for a loop. a for loop is just a while loop that does some extra stuff automatically for you.
Code:
for(int tries=1;tries <=3;tries++){
cout <<"Enter number" << tries << endl;
cin >> number;
if(number == mynumber)
{
cout <<"You got my number correct!"<< endl;
break;
}
else if(number != mynumber)
{
cout <<"Your number is incorrect!"<< endl;
}
}

The for loop will repeat from 1 to 3 taking in a number and comparing it to the random number. if they're equal, it'll say you win and "break;". This means it will break out of the for loop and go down to the system("PAUSE") and return. if they're not equal, it will say that and start the loop again.

Once you understand this and want to improve, try seeing if you can tell them whether the number is higher or lower than what they put in.


Ty for the help, very appreciated. Smile

Edit: I'm getting an error saying ' error C3861: 'clock': identifier not found'

Any idea why?
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: Thu Jul 03, 2008 1:23 pm    Post subject: Reply with quote

try Clock() instead of clock(). and make sure you have the (). It's a function, not just a variable.
_________________
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jul 03, 2008 1:25 pm    Post subject: Reply with quote

MegaForum wrote:


Any idea why?


Just use rand() and seed it with GetTickCount() or something.
Back to top
View user's profile Send private message
MegaForum
Grandmaster Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 558

PostPosted: Thu Jul 03, 2008 1:26 pm    Post subject: Reply with quote

HalfPrime wrote:
try Clock() instead of clock(). and make sure you have the (). It's a function, not just a variable.



Nope same error except says 'Clock'. Also , should i have declaired it as an int as so

Code:

int mynumber=(Clock().%99)+1;



and then fill in each spot where its comparing the two, put mynumber?


Edit: slovach says: Just use rand() and seed it with GetTickCount() or something

No idea what that is Razz Just because I'm getting back into it, doesn't mean I know all the available concepts XD
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jul 03, 2008 3:19 pm    Post subject: Reply with quote

Code:
#include <windows.h>
#include <iostream>

void main( void )
{
    int random, i, guess;
    //-------

    srand( GetTickCount() );
    random = rand()%99 + 1;
    std::cout << "You have 3 tries to guess the number! Begin..." << std::endl;
    for( i = 0; i < 3; i++ ) {
        std::cout << "Guess " << i+1 << ": ";
        std::cin >> guess;
        if( guess > random ) std::cout << "Too high!" << std::endl;
        else if( guess < random ) std::cout << "Too low!" << std::endl;
        else break;
    }
    std::cout << ((i == 3) ? "You lose!" : "You win!") << std::endl;
    std::cin.sync();
    std::cin.ignore();
}
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Thu Jul 03, 2008 3:38 pm    Post subject: Reply with quote

why not use srand(time(0));? Then you don't need windows.h
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jul 03, 2008 3:40 pm    Post subject: Reply with quote

noz3001 wrote:
why not use srand(time(0));? Then you don't need windows.h


Then you need to include ctime, and windows.h is a far more common header.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Thu Jul 03, 2008 4:46 pm    Post subject: Reply with quote

Flyte wrote:
noz3001 wrote:
why not use srand(time(0));? Then you don't need windows.h


Then you need to include ctime, and windows.h is a far more common header.


windows.h is bigger and is OS dependant.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 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