user5413427
user5413427

Reputation: 41

Guessing game where computer generates random number

I am implementing a Guessing game where computer generates random number with the following code:

int main()
{
    srand(time(NULL));

    while (true){
        int num = rand() % 10, guess, tries = 0;        

        while (true){
            cout << "Enter number 1 to 10:";
            cin >> guess;

            if (tries > 2)
            {
                break;
            }

            if (guess > num)
            {
                cout << "Too High ! Try again"<<endl;

            }

            if (guess > 10)
            {
                cout << "Error ReEnter 1 to 10\n";
            }
            else if (guess < num)
            {
                cout << "Too Low! Try again"<<endl;
            }
            else
            {
                break;
            }
            tries++;            
        }

        if (tries > 2)          
        {
            cout <<"\nYou ran out of tries!\n";
            cout << "\nThe answer is:" << num << endl;
        }
        else
        {
            cout << "\nCONGRATZ!! You guess correctly!\n";
        }       
        return 0;
    }
}

One of the problems is: yet when user attempt 3 times, the program shows "ran out of tries" even though the user input is correct on 3rd try.

Questions:

1.How do I inform user that their input exceeds 10 and show an error message to user to enter values from 1 to 10 ?

2.How to correct the aforementioned problem?

Upvotes: 2

Views: 1134

Answers (2)

AndersK
AndersK

Reputation: 36082

instead of writing the program for you here is some pseudo code.

get a random number rand()%10+1  1..10 call it R
loop
  get user input N
  if N == R then show OK and break loop
  if N < R show too low 
  else show too high
  increment tries
  if tries == 3 then break loop
end loop

Upvotes: 1

Ziezi
Ziezi

Reputation: 6467

You have too many if else conditions that make your code unnecessarily complex, to answer your second question specifically the unwanted behaviour is caused from the:

  if (tries > 2)
  {
      break;
  }

which exits the loop regardless of the guess, as it is dependant only on the number of tries. Regarding your first question, I decided to provide you with more simple implementation that includes an answer to it as well.

You could replace your while loop with a do-while loop, terminated when the random number is guessed, i.e.:

int main(){
    // initialize random seed
    srand (time(NULL));
    // generate a random number within [1,10]
    int secretRandom = rand() % 10 + 1;
    // initialize 
    int yourGuess = 11;

    // input loop
    string promptMessage = "Type a a number from 1 to 10."
    do{
        cout << promptMessage << '\n';
        // read input
        cin >> yourGuess >> endl;
        // guessed number relatively to the randomly generated  
        if (secretRandom < yourGuess) cout << "The secret number is lower\n";
        else if (secretRandom > yourGuess)  cout << "The secret number is higher\n";
    }while(yourGuess != secretRandom) 
    // if you guess the random number exit the loop and display success message
    cout << "You guessed right!\n";

return 0;
}

To reduce the amount of guesses to specific number you can enclose the do-while loop and the success message in a for loop, for example:

int numberOfGuesses = 3;
for (int i = 0; i <= numberOfGuesses; ++i){
    //...
} 

If you want to enforce the user to input a number in the range from 1 to 10, you could do it by:

int yourGuess = 11;
int lowerBound = 0;
int upperBound = 10;
do{
    cin >> yourGuess;
    // not type safe
}while(yourGuess < lowerBound || yourGuess > upperBound);

Upvotes: 0

Related Questions