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]
4 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]4 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
In the replace dialog box, clicking what button changes highlighted text
Andre45 [30]

ANSWER: Replace Button

EXPLANATION: After opening the replace dialog box and putting the desired values in the boxes, we need to click the 'Replace' button to change the highlighted text. If the word which needs to be changed appear multiple times in the document, then clicking on 'Replace All' is advised as it would change all the old words with the new ones.

7 0
3 years ago
Read 2 more answers
A programming language is used to tell a computer what to do to solve a problem?
goldfiish [28.3K]
As far as humans are concerned. What's written in the programming language needs to interpreted or compiled into something that the machine can actually deal with.
5 0
3 years ago
Microsoft s trash receptacle is called
solmaris [256]
The term that is being looked for in this problem would be most probably and most likely to be the Recycle Bin.

Microsoft's trash receptacle is called the Recycle Bin. This is where deleted files and folder are moved after being deleted. Simple selecting the file or files and pressing the delete key would prompt you to make sure that you really wanted to do this. This is a precautionary action of the computer to let you know what you are about to do. This is to make sure that errors wouldn't happen in the future since some files are highly sensitive, some programs also are, and some files are just too big for the Recycle Bin to be handled, thus they are deleted permanently.
3 0
4 years ago
The symbol is used to indicate that a line of text is a comment.
Vladimir [108]

Answer:

Explanation:

asterisk

6 0
3 years ago
Read 2 more answers
HELP 10 POINTS AND BRAINLIEST FOR BEST ANSWER! EASY I PROMISE! HELPFUL ANSWERS ONLY PLEASE HURRY!
tiny-mole [99]

Answer:

WATCH

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • If you see ##### in a cell, you should
    10·1 answer
  • When it comes to the best possible security for your wireless router, be sure to use WEP encryption to ensure that your transmis
    14·1 answer
  • The small gear that is driven by the motor shaft of a rotisserie is called a?
    9·1 answer
  • Why is failure important when you are designing a solution to a problem?
    6·2 answers
  • Why concurrency control is needed in transaction.
    5·1 answer
  • Do you humans know what math is?
    9·1 answer
  • Select the correct answer.
    7·2 answers
  • PLEASE HELP
    12·1 answer
  • Which is an example of a demand account
    10·1 answer
  • A__ is a part of a GUI that allows a specific type of interaction with the user.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!