I cannot quit after the program went to loop and if user choose not to continue game 'n' the program keeps asking for new guess.
heres the code

#include <iostream>
#include <iomanip>
//#include <stdlib>
#include <ctime> //or <ctime>

using namespace std;

int main()
{
    //local constants
    const int MAX_NUM  = 20;
    const int MIN_NUM  = 1;
    const int SENTINEL = -1;
    const char YES     = 'y';
    const char NO      = 'n';
    
    //local variables
    int Guess=0;
    int Num1;
    int Random;
    int Num_Of_Guesses =1;
    char Answer;
    bool Play_Again = true;
/*********************************************************/
//Title
cout << "\n";
cout << setw(46) << "Guessing Game" << endl;
cout << setw(46) << "-------------";
cout << "\n\n";
     //Ask for a number 
      cout << setw(59) << "Guess the number (1-20) or -1 to Quit: ";
      cin  >> Guess;

while(Play_Again)
{

             
//Seed the random generator
srand(time(0));

//Create a randome number between 1 - 20
Random = rand() % MAX_NUM + 1;

     if (Guess == SENTINEL)
     {
       system("cls");      
       cout << setw(59) << "Number was       :  " << Random;
       cout << "\n\n";
       system ("PAUSE");
       }   

    //Check if input doesnt equal the guess #        
    while (Guess != Random && Guess !=SENTINEL)
    {

         //Display message when # higher
         if ( Guess > Random )
         {
            
            cout << setw(43) << "Too High" << endl;
            
            cout << setw(59) << "Guess again or -1 to Quit            : ";
            cin  >> Guess;
            Num_Of_Guesses++;
         }
        
         //Display message when # lower
         if( Guess < Random )
         {
            
            cout << setw(43) << "Too Low" << endl;
            
            cout << setw(59) << "Guess again or -1 to Quit            : ";
            cin  >> Guess;
            Num_Of_Guesses++;
         }
    }
     
     
    //When the input equals random #
    if (Guess == Random)
    {
       system("cls");      
       cout << "\n";
       cout << setw(53) << "You guessed correctly!";
       cout << "\n\n";
       cout << setw(59) << "The Random Number Was                : " << Random;
       cout << "\n";
       cout << setw(59) << "Number of Guesses                    : " 
            << Num_Of_Guesses;
       cout << "\n\n";
       system ("PAUSE");
       
       //Clear the screen
       system("cls");
       cout << "\n\n";
       cout << setw(55) << "Continue Game? Enter y or n: ";
       cin  >> Answer;
       cout << "\n";
       Num_Of_Guesses = 1;
      //Ask for a number 
      cout << setw(59) << "Guess the number (1-20) or -1 to Quit: ";
      cin  >> Guess;
    } 

    //Ask if user wants to play again
    if(Answer!='y')
       Play_Again = false;
       
}

  
    return 0;
}//end main program

help fast would be appreciated..

Your program flow is a little off. You want to have an outer loop that you go through once per game and an inner loop that you go through once per guess. You get out of the inner loop by guessing correctly or deciding you don't want to guess anymore. You get out of the outer loop by deciding you don't want to play anymore.

Thus you should ask the user for the guess only if they still want to play. Therefore if you ask whether they want to play again on line 97, don't ask for a guess on line 102 without checking the answer from line 97. Right now you ask for a guess regardless. Either stick line 102 somewhere else or execute it only if the user says they want to play again.

lol yea i figured it out already but thank you anyways

Edited 6 Years Ago by nickx522: n/a

Another problem. srand() should only be called once during program execution. Move the function call to the beginning of main()

// This program asks the user to input a number from 1 to 20

// as it produces a random number, and it informs the user on

// how relatively close they are to the number.





#include <cstdlib>

#include <iostream>



using namespace std;

const int BUFFER_SIZE = 10;

int main()

{

   int counter = 0,                       // Variable to store number of tries
       theNumber,                         // The Random number in memory
       theGuess;                          // The User input

   char keepOn;
   
   do
   {
   srand ( time(NULL) );



   theNumber = rand() % 20 + 1;          // The random number from 1-20

   



   

   do

   {
       system("cls");
       cout << "Guessing Game" << endl;
       cout << "-------------" << endl;
       cout << "I am thinking of a number from 1 to 20, please guess that number or 0 to exit: " 

       << endl;                           // Ask for the user input
       
       while (!(cin >> theGuess))
       {
             char ch;
             cin.clear();
             cout << "Please enter only numbers or 0 to exit: " << endl;
             endl(cout);
             while (cin.get ( ch ) && ch != '\n' );
       }                                    // Ignore inproper input

       counter++;                         // Increase to the variable per loop

       if (theGuess == 0)
       {
          return EXIT_SUCCESS;
       }
       else if (theNumber > theGuess)                    // If stat. for number being low
       {
          endl(cout);
          cout << "Number is too low, please try again or 0 to exit." << endl;
          endl(cout);
          system("pause");
          system("cls");                  // clears screen
       }
       else if (theNumber < theGuess)               // If stat. for number being high
       {
          endl(cout);
          cout << "Number is too high, please try again or 0 to exit." << endl;
          endl(cout);
          system("pause");
          system("cls");     
       }
       

       

   }

   while(theGuess != theNumber);                    // Run loop again if not == to number

   
   endl(cout);
   cout << "Congratulations, you found the number in " << counter << " tries." 

   << endl;                               // Congratulate user upon success
   endl(cout);
   cout << "Do you want to play again? Y or N " << endl;
   endl(cout);
   while (!(cin >> keepOn))
       {
             char ch;
             cin.clear();
             endl(cout);
             while (cin.get ( ch ) && ch != '\n' );
       }     
   endl(cout);
}
while ( keepOn == 'y' || keepOn == 'Y');

   return EXIT_SUCCESS;

}

Compare and make your code more efficient.

Edited 6 Years Ago by Nandomo: n/a

This article has been dead for over six months. Start a new discussion instead.