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 


[C++] Need new method..
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
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu Jul 10, 2008 2:05 am    Post subject: [C++] Need new method.. Reply with quote

Well I'm just trying to make a simple "Guess the number" program.

Code:
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;

void GoHigher()
{
 do cout << "Guess Higher" <<endl;
   while (random_integer > guess);
goto loop:
}


void GoLower()
{
 do cout << "Guess Lower" <<endl;
  while (random_integer < guess);
goto loop:
}

int main()
  {
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
int guess;
cin >> guess;
if (random_integer == guess) cout << "Congratz! You got the number!" << endl;
else if (random_integer > guess); goto GoHigher()
else (random_integer < guess); goto GoLower()

std::cin.sync();
std::cin.ignore();           
    return 0;
}



I'm sure you can tell what I'm trying to do, but the way I wrote it is shit Confused Obviously I need to implement a loop, but which would be best? Secondly I was looking at this http://www.daniweb.com/forums/thread1769.html and I was wondering, how do I set RAND_MAX to 100? I want my guess the number to be in between 1-100? And how do I use srand rather than rand?

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

Joined: 29 Dec 2006
Posts: 804

PostPosted: Thu Jul 10, 2008 4:28 am    Post subject: Re: [C++] Need new method.. Reply with quote

manc wrote:
Obviously I need to implement a loop, but which would be best?
Depends on what kind of loop do you want to make. For infinite looping, use for(;;), for looping the code while some variable is true, I'd use (do-)while-loop.

manc wrote:
how do I set RAND_MAX to 100? I want my guess the number to be in between 1-100?
Code:
int value = (::rand()%100)+1;


manc wrote:
And how do I use srand rather than rand?
Just call it with a "random" seed. The time works the best, since it changes often and won't be the same in future.
Code:
#include <ctime>
#include <cstdlib>

::srand ( time(NULL) );
int value = ::rand();
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Thu Jul 10, 2008 8:34 am    Post subject: Reply with quote

I don't think you can use srand by itself.
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu Jul 10, 2008 11:05 am    Post subject: Reply with quote

FORGET THE POST AT THE TOP

Ok, so I dont understand whats wrong with this one..I'm really struggling here =\.

Code:
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;

int main()
  {
       
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
cin >> guess;


do
    {
     cout << "Congratz! You got the number!" << endl;
    } while (random_integer == guess);

do
    {
     cout << "Guess Higher" << endl;
     goto loop;
    } while (random_integer > guess);
   
   
do
    {
     cout << "Guess Lower" << endl;
    goto loop;
    } while (random_integer < guess);

std::cin.sync();
std::cin.ignore();
}

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Thu Jul 10, 2008 12:22 pm    Post subject: Reply with quote

Whoa... firstly, try to avoid goto at all costs. It's not worth using it. Here's a quick example to achieve the same effect you are going for:

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

int main( int argc, TCHAR* argcv[] )
{
   int      iNumber = NULL;
   int      iGuess   = NULL;

   do{
      srand( GetTickCount() );
      iNumber = ( rand() % 100 ) + 1;
   }while( iNumber == 0 );

   std::cout << "Number Guessing Game" << std::endl;
   std::cout << "I have generated a random number between 1 and 100. Try to guess it!" << std::endl;
   std::cout << "If you wish to exit the game, simply enter 0." << std::endl;
   
   for(;;)
   {
      std::cout << "Enter your guess: ";
      std::cin >> iGuess;

      if( iGuess == iNumber )
      {
         std::cout << "You guessed the number!" << std::endl;
         break;
      }
      else if( iGuess > iNumber )
      {
         std::cout << "Guess lower.." << std::endl;
      }
      else if( iGuess < iNumber )
      {
         std::cout << "Guess higher.." << std::endl;
      }
      else if( iGuess == 0 )
      {
         break;
      }
   }
   
   std::cin.sync();
   std::cin.ignore();
   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
kitterz
Grandmaster Cheater Supreme
Reputation: 0

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Thu Jul 10, 2008 12:26 pm    Post subject: Reply with quote

Code:
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;

int main()
  {
       
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
random_integer = rand();

   while (1==1){
      cout << "Take a guess" << endl;
      cin >> guess;

       if (random_integer == guess)
       {
          cout << "Congratz! You got the number!" << endl;
       }

        if (random_integer > guess)
       {
          cout << "Guess Higher" << endl;
       }

         if (random_integer < guess)
       {
          cout << "Guess Lower" << endl;
       }

}
std::cin.sync();
std::cin.ignore();
}


Edit: Things to remeber

1. Avoid loop:
2. Use if statements
3. Do not declare the same variable twice.
Back to top
View user's profile Send private message Send e-mail
Noz3001
I'm a spammer
Reputation: 26

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

PostPosted: Thu Jul 10, 2008 12:36 pm    Post subject: Reply with quote

Yeah, goto shouldn't be used like that
Back to top
View user's profile Send private message MSN Messenger
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Thu Jul 10, 2008 3:21 pm    Post subject: Reply with quote

manc wrote:
FORGET THE POST AT THE TOP

Ok, so I dont understand whats wrong with this one..I'm really struggling here =\.

Code:
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;

int main()
  {
       
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
cin >> guess;


do
    {
     cout << "Congratz! You got the number!" << endl;
    } while (random_integer == guess);

do
    {
     cout << "Guess Higher" << endl;
     goto loop;
    } while (random_integer > guess);
   
   
do
    {
     cout << "Guess Lower" << endl;
    goto loop;
    } while (random_integer < guess);

std::cin.sync();
std::cin.ignore();
}


whoa, you can't have multiple "do's" in a program, unless they are embedded in one another.
Back to top
View user's profile Send private message MSN Messenger
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Thu Jul 10, 2008 3:25 pm    Post subject: Reply with quote

Yeah lol my bad. Final result:
Code:

#include "windows.h"
#include <iostream>
#include "stdio.h"

using namespace std;

//-----------------------------------------------

int Guess, Generated_num, intcheck;

char w_h[] = " Guess Higher! ";

char w_l[] = " Guess Lower! ";

char correct[] = " You guessed right! Start a new game? [1:Yes/2:No] ";

//-----------------------------------------------

int check(int inGuess, int RandomNum)
{
if(inGuess != RandomNum)
   {
   if(inGuess > RandomNum)
      return 1;
   if(inGuess < RandomNum)
      return 2;
   }
return 3;
}

//-----------------------------------------------

int main()
{
cout << "Welcome to guess the number." << endl;

cout << "The secret number lies within 1-100" << endl;

::srand(time(NULL));

newgamejump:

Generated_num = (::rand()%100)+1;

incorrectjump:

cout << " Guess : " ;

cin >> Guess;

intcheck = check(Guess, Generated_num);

   switch(intcheck)
   {
   
      case 1:

      cout << w_l << endl;

      goto incorrectjump;

       case 2:
      
      cout << w_h << endl;
      
      goto incorrectjump;
      
        case 3:

      cout << correct << endl;

      cin >> intcheck;

      if(intcheck == 1)   

      goto newgamejump;

      
   }

return 0;
}



With alot of help from slippppppp

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Thu Jul 10, 2008 9:15 pm    Post subject: Reply with quote

manc wrote:
Yeah lol my bad. Final result:
Code:

#include "windows.h"
#include <iostream>
#include "stdio.h"

using namespace std;

//-----------------------------------------------

int Guess, Generated_num, intcheck;

char w_h[] = " Guess Higher! ";

char w_l[] = " Guess Lower! ";

char correct[] = " You guessed right! Start a new game? [1:Yes/2:No] ";

//-----------------------------------------------

int check(int inGuess, int RandomNum)
{
if(inGuess != RandomNum)
   {
   if(inGuess > RandomNum)
      return 1;
   if(inGuess < RandomNum)
      return 2;
   }
return 3;
}

//-----------------------------------------------

int main()
{
cout << "Welcome to guess the number." << endl;

cout << "The secret number lies within 1-100" << endl;

::srand(time(NULL));

newgamejump:

Generated_num = (::rand()%100)+1;

incorrectjump:

cout << " Guess : " ;

cin >> Guess;

intcheck = check(Guess, Generated_num);

   switch(intcheck)
   {
   
      case 1:

      cout << w_l << endl;

      goto incorrectjump;

       case 2:
      
      cout << w_h << endl;
      
      goto incorrectjump;
      
        case 3:

      cout << correct << endl;

      cin >> intcheck;

      if(intcheck == 1)   

      goto newgamejump;

      
   }

return 0;
}



With alot of help from slippppppp


You are still doing bad things. Don't use goto, it's not good to use it ever, there are ways to code things to never need it. Take a look at the example I posted above, it will achieve what you are trying to do, using a small if/then case and loop until the user either wants to exit or guesses the number.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jul 10, 2008 10:16 pm    Post subject: Reply with quote

There is a use for goto, to abort nested loops:

Code:
for(;;) {
    for(;;) {
        goto out;
    }
}
out:


Anyways, here is what you are looking for in proper C++, with error checking.

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

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

    ::srand( ::GetTickCount() );
    random = ::rand()%100 + 1;
    std::cout << "Guess the number between 1-100. Begin..." << std::endl;
    for( i = 1;; i++, std::cin.sync() ) {
        std::cout << "Guess " << i << ": ";
        std::cin >> guess;
        if(std::cin.fail()) {
            std::cin.clear();
            std::cout << "Invalid input." << std::endl;
        } else if( guess > random ) std::cout << "Too high!" << std::endl;
        else if( guess < random ) std::cout << "Too low!" << std::endl;
        else break;
    }
    std::cout << "You win!" << std::endl;
    std::cin.sync();
    std::cin.ignore();
}
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Fri Jul 11, 2008 1:54 am    Post subject: Reply with quote

Wiccaan wrote:
Whoa... firstly, try to avoid goto at all costs. It's not worth using it. Here's a quick example to achieve the same effect you are going for:

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

int main( int argc, TCHAR* argcv[] )
{
   int      iNumber = NULL; //Why is it assigned NULL?
   int      iGuess   = NULL; //Why is it assigned NULL?

   do{
      srand( GetTickCount() );  // What is/does GetTickCount do?
      iNumber = ( rand() % 100 ) + 1;
   }while( iNumber == 0 );

   std::cout << "Number Guessing Game" << std::endl;
   std::cout << "I have generated a random number between 1 and 100. Try to guess it!" << std::endl;
   std::cout << "If you wish to exit the game, simply enter 0." << std::endl;
   
   for(;;)
   {
      std::cout << "Enter your guess: ";
      std::cin >> iGuess;

      if( iGuess == iNumber )
      {
         std::cout << "You guessed the number!" << std::endl;
         break;
      }
      else if( iGuess > iNumber )
      {
         std::cout << "Guess lower.." << std::endl;
      }  //what makes this jump back to "Enter Your Guess"?
      else if( iGuess < iNumber )
      {
         std::cout << "Guess higher.." << std::endl;
      } //what makes this jump back to "Enter Your Guess"?
      else if( iGuess == 0 ) // er..???
      {
         break;
      }
   }
   
   std::cin.sync();
   std::cin.ignore();
   return 0;
}


I dont understand the parts I commented on. Why are they assigned NULL? And what is GetTickCount? Lastly, in the else if statements, after it says Guess Higher/Lower, what makes it jump back to "Enter your guess" ?



Flyte wrote:


Anyways, here is what you are looking for in proper C++, with error checking.

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

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

    ::srand( ::GetTickCount() );  //What is GetTickCount?
    random = ::rand()%100 + 1;
    std::cout << "Guess the number between 1-100. Begin..." << std::endl;
   for( i = 1;; i++, std::cin.sync() ) {   //What..
        std::cout << "Guess " << i << ": "; //If i hasnt been assigned a value yet..then what?
        std::cin >> guess;
        if(std::cin.fail()) { // especially lost on this part
            std::cin.clear(); // and this part
            std::cout << "Invalid input." << std::endl;
        } else if( guess > random ) std::cout << "Too high!" << std::endl;// once again what makes it jump back?
        else if( guess < random ) std::cout << "Too low!" << std::endl; //
        else break;// same question
    }
    std::cout << "You win!" << std::endl;
    std::cin.sync();
    std::cin.ignore();
}


I know I'm a nuisance, thanks for the help though.

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

Joined: 29 Dec 2006
Posts: 804

PostPosted: Fri Jul 11, 2008 6:38 am    Post subject: Reply with quote

Flyte wrote:
Anyways, here is what you are looking for in proper C++, with error checking.
Fails:
Code:
Guess the number between 1-100. Begin...
Guess 1: Invalid input.
CTRL-C fails :<

And I'd prefer
Code:
::srand( ::time(NULL) );
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Fri Jul 11, 2008 6:54 am    Post subject: Reply with quote

Quote:
I dont understand the parts I commented on. Why are they assigned NULL? And what is GetTickCount? Lastly, in the else if statements, after it says Guess Higher/Lower, what makes it jump back to "Enter your guess" ?


GetTickCount is an API, I was only using it instead of ::time() to avoid an extra include for the example. I'd use ::time() as well as it will provide a more random seed. GetTickCount is just the amount of time in milliseconds your computer has been turned on.

As for jumping back, my example was inside an endless for loop. for(;Wink so when the program reaches the end of the loop, it goes back and starts over.

Code:
for(;;) // <-- Loop forever
{
  // Start of the for loop here..

  // Code to do what ever here..

  // End of the for loop here..
} // <-- End of loop, after we get here we jump back to the start of the for code.

_________________
- Retired.
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: Fri Jul 11, 2008 11:39 am    Post subject: Reply with quote

Oh ok, what about assigning NULL to iNumber and iGuess?

And then Its an endless loop because of (;;) ?

_________________
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 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