I am new to programming in DEV-C++ so I ran in to a few problems with my code, I am trying to create a number guessing game if you run my code its working well but I also need to have an option of "PLAY AGAIN" at the end which will restart a program with a new random number and also need to have an error trapping so it will only allow me to input numerical values. If you could help I would appreciate that. Thank you!!!

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{   
    int level;
    int guess;
    int randn;
    int chances=3;
    system("title Advanced Number Guessing Game");
    system("color 5");
    
    cout<<"Welcome to the Advanced Number Guessing Game!!\n"<<"**********************************************\n";
    
    cout<<"\n1)Easy (1-10)\n";
    cout<<"2)Medium (1-20)\n";
    cout<<"3)Hard (1-30)\n";
   
    cout<<"\nChoose level of difficulty:";
           cin>>level;
      
    switch (level)     
    case (1):  
        srand(time(0));
        randn= rand()% 10 + 1;    
        system("cls");
           
         do
    {
        cout<<"Please enter a number between 1 and 10 (including 1 and 10):";
        cin >> guess;
        chances--;

        if(guess == randn)                            
         cout << "\nTHAT IS CORRECT!! YOU WIN!! \n"<<"Thank you for playing the Number Guessing Game!\n";
        else if (chances == 0)                    
        cout << "\nSorry, you did not guess the number.  You Lose!!\n"<<"The correct number was \n"<<randn;     
        else    
    {       
      if (guess!=randn && guess>randn)                                    
             cout << "Sorry, thats too high.  Please try again.  You have "<<chances<<" more chances:\n";
                if (guess!=randn && guess<randn)
            cout << "Sorry, thats too low.  Please try again.  You have "<<chances<<" more chances:\n";     
    }}while(chances>0 && guess!=randn);
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

Wrap everything around in a do while loop;

char quit = false;
int guessNumber = 0;
srand(time(0));
guessNumber = rand() % 100 + 1;
do
{
   PlayGame();

   cout<< "quit <1 for yes or 0 for no > : ";
   cin >> quit;
  if( quit == false ) { guessNumber = rand()%100 + 1; }

}while(quit == false)

What first person has said is right about the wrapping but the example given is wrong as it quits the loop on every input 0 or 1(at least according to gcc 4.3).
the error is in the line:

char quit = false;

You cannot use a character variable to hold a boolean value.So to correct this mistake and to successfully convert the above statement to either of these:

bool quit = false;
//or
int quit = false;

It also never gets a new number. The line guessNumber = rand() % 100 + 1; Needs to be inside the loop.

Opps, thanks Csurfer, its was late when I posted, I think.

@waltP, what do you mean, guessNumber never changes?

if( quit == false ) { guessNumber = rand()%100 + 1; }

When the game is near its end it changes if player wan'ts to play again.

Also the guessNumber should have been a parameter passed into
PlayGame function, sorry.

Thats what I get for staying up late.

@waltP, what do you mean, guessNumber never changes?

if( quit == false ) { guessNumber = rand()%100 + 1; }

When the game is near its end it changes if player wan'ts to play again.

Sorry. I didn't see that code because it didn't make sense to me at the end, and in that form. It's best at the beginning of the loop in this case.

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