Ask Question

Write a program that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display Too high. Try again. If the user's guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until the user correctly guesses the random number. Then the program should display the number of guesses along with the following message Congratulations. You figured out my number. Suggest that you also give the user the opportunity to play the game again or quit.

+2
Answers (1)
  1. 29 April, 15:20
    0
    Answer: The c+ + program for the number guessing game is given below.

    #include

    using namespace std;

    / / method declaration without parameters

    void game ();

    int main () {

    char reply;

    / / calling the game method

    game ();

    cout<<"Would you like to continue with the game? Enter y to continue. Enter q to quit the game. " <
    cin>>reply;

    if (reply = = 'y')

    game ();

    if (reply = = 'q')

    cout<<"Quitting the game ... "<
    return 0;

    }

    void game ()

    {

    int num, guess, attempt=0;

    num = (rand () % 10) + 6;

    cout<
    do

    {

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;

    if (guess15)

    {

    do

    {

    cout<<"Invalid number. Enter any number between 5 and 15. "<
    cin>>guess;

    }while (guess15);

    }

    if (guess < num)

    {

    cout<<"Too Low"<
    attempt++;

    }

    if (guess > num)

    {

    cout<<"Too High"<
    attempt++;

    }

    }while (guess! = num);

    cout<<"Congratulations. You figured out my number in "<
    }

    Explanation:

    The expression

    (rand () % 10)

    generates a random number between 0 and 9. 6 is added to generate a random number between 5 and 15.

    The integer variable attempt is initialized to 0.

    Next, user is asked to guess the number. If the user enters an invalid input, the do-while loop begins until a valid input is received from the user.

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;

    if (guess15)

    {

    do

    {

    cout<<"Invalid number. Enter any number between 5 and 15. "<
    cin>>guess;

    }while (guess15);

    }

    User is prompted to input the guess using the same do-while loop as above. Until the user guesses the correct number, the loop continues. Each incorrect guess by the user, increments the variable attempt by 1. This represents the number of attempts done by the user to guess the correct number.

    if (guess < num)

    {

    cout<<"Too Low"<
    attempt++;

    }

    if (guess > num)

    {

    cout<<"Too High"<
    attempt++;

    }

    The random number generation and the guessing logic is put in a separate method game ().

    The entry and exit from the game only is put inside the main method.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user's guess is ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers