Results 1 to 3 of 3
  1. #1
    PairyHenis69's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    40
    Reputation
    10
    Thanks
    6

    guess the number game help

    i made this game where the user has to guess the number that the computer chose at random. i had it working perfectly, then i wanted to make it so that the user can loop and restart the game. here is my code:

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
     
    int main ( )
    {  
      srand (time (NULL) );
      
      int Secret_Number = rand ( ) % 24 + 1;
      char YES = 'y';
      int Guess = 0;
      float num1 = 1;
      int choice = 3;
      int game;
    
      do
      {
    	  cout << "In this game, you will be guessing a random number between" << endl;
    	  cout << "0 and 24 that the computer has chosen." << endl;
    	  cout << "Do you want to play the game? press 'y' to play." << endl;
    	  cin >> choice;
    	  if(choice == YES)
    	  {
    		  cin >> game;
    	  }
    	  else
    	  {
    		  return 0;
    	  }
    	  cout << "----------------------------------------------------------" << endl;
    
    		  while(game)
    		  {
    			  cout << "Guess the random number." << endl;
    			  cin >> Guess;
    		 
    			  if (Guess > Secret_Number)
    			  {
    				 cout << "Your guess is too high." << endl;
    			  }
    		 
    			  if (Guess < Secret_Number)
    			  {
    				 cout << "Your guess is too low." << endl;
    			  }
    		 
    			  if (Guess == Secret_Number)
    			  {
    				 cout << "Good job! You guessed the secret number!" << endl;
    				 cout << "Thanks for playing." << endl;
    			  }
    		  }
    		  cout << "Do you want to play the game? press 'y' to play." << endl;
    		  cin >> choice;
    		  if (choice == YES)
    		  {
    			  continue;
    		  }
    		  else
    		  {
    			  cout << "Thanks for playing!" << endl;
    			  break;
    		  }
      }while (Guess != Secret_Number);
      system ("PAUSE");
    }
    when i debug it, it just closes. help?
    and thanks in advance!

  2. #2
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    The variable 'game' has nothing changing it. (besides the first if that I don't manage to understand)
    And 'choice' is a integer, not a char. (Same shit, but oh well, you will figure that out later)

    Also, instead of the Continue/Break on the infinite loop... Include the condition on the While(...) it self. (like you do on the first). Thats not a good practice of programming.

    And it will close because your "return 0", at the else block.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  3. The Following User Says Thank You to 'Bruno For This Useful Post:

    PairyHenis69 (10-10-2012)

  4. #3
    PairyHenis69's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    40
    Reputation
    10
    Thanks
    6
    /solved.

    thanks for ur help
    Last edited by PairyHenis69; 10-10-2012 at 10:59 PM.

Similar Threads

  1. [Release] A Very Basic Game Guess The Number Game™
    By Dead(H)ell in forum Visual Basic Programming
    Replies: 7
    Last Post: 03-23-2012, 02:40 AM
  2. [Release] Guess That Number Game
    By sherkun in forum Visual Basic Programming
    Replies: 11
    Last Post: 11-17-2010, 02:32 AM
  3. The Number Game
    By Austin in forum Spammers Corner
    Replies: 7
    Last Post: 06-17-2010, 03:38 AM
  4. THE NUMBER GAME!!!!
    By zebramanz in forum General
    Replies: 27
    Last Post: 03-29-2009, 11:09 PM