1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
juin [17]
3 years ago
5

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 gue

ss 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.
Computers and Technology
1 answer:
stepan [7]3 years ago
4 0

Answer: The c++ program for the number guessing game is given below.

#include <iostream>

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. " <<endl;

cin>>reply;  

if(reply == 'y')

    game();      

if(reply == 'q')

    cout<<"Quitting the game..."<<endl;  

return 0;

}

void game()

{

   int num, guess, attempt=0;  

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

cout<<endl<<"Welcome to the game of guessing the correct number "<<endl;  

   do

{    

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

    cin>>guess;      

    if(guess<5 || guess>15)

    {

        do

        {

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

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

    if(guess < num)

    {

        cout<<"Too Low"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        attempt++;          

    }      

}while(guess != num);  

cout<<"Congratulations. You figured out my number in "<<attempt<< " attempts."<<endl;

}

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(guess<5 || guess>15)

    {

        do

        {

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

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

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"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        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.

You might be interested in
Donde veo haikyuu en español sin lag ni virus :C (la segunda, tercera y cuarta temporada)
Mars2501 [29]

Answer:

keep in English language

Explanation:

keep in English

3 0
2 years ago
Which of the following are devices that work essentially like a pen and paper, allowing the user to input drawings on a computer
DochEvi [55]
The device that work essentially like a pen and paper, allowing the user to input drawings on a computer is graphic tablet. The first option is correct.
3 0
3 years ago
X.C. Who wrote the poem "The Road Not Taken"?
zavuch27 [327]

Answer:

Robert Frost

Explanation:

5 0
3 years ago
Read 2 more answers
Select all reasons it is important to perform equipment maintenance
Blizzard [7]

Answer: 1.to avoid equipment downtime.

2.To keep equipment running accurately

3. To avoid defects

4. To keep business professionals busy with maintenance procedures

5. To avoid losing data

6. To keep computers running slowly

7. To maximize productivity

Explanation:r

6 0
3 years ago
Which feature is a benefit of using digital cable?
Natasha_Volkova [10]

Answer:

A low power usage.

Explanation:

because its easy

8 0
3 years ago
Read 2 more answers
Other questions:
  • Which is an example of intrinsic motivation?
    13·2 answers
  • Your dive computer indicates you need to make a mandatory decompression stop. You buddy's dive computer does not. You should: A.
    9·1 answer
  • The ____, or typeface, defines the appearance and shape of letters, numbers, and special characters.
    15·1 answer
  • Each 4G device has a unique Internet Protocol (IP) address and appears just like any other wired device on a network
    14·1 answer
  • Which is the hanging indent on the ruler?
    10·2 answers
  • In relation to data science,advances in technology has made it more feasible to do what
    5·1 answer
  • What symbol do we use to denote a character?
    14·1 answer
  • Which of the following correctly stores 45 squared in the variable x?
    11·1 answer
  • What does computer graphics mean?​
    11·1 answer
  • You are working at the help desk and you get a message that a user cannot access the internet. you open a command prompt, ping t
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!