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
. wireless network adapters differ from regular networkadapters because theycontain………………..
Nataly_w [17]

Answer: Transceivers

Explanation:

 Wireless network adapters differ from regular network adapters because they contain transceivers as, the transceiver is that portion of the network interface which transmitted the data by concerting digital data into the digital signals in the medium. Basically, the network adapter are responsible for connected the host in the network medium and also convert the binary form data.Transceiver signal are basically depends upon the type of the network.

8 0
3 years ago
What is a curson?
sp2606 [1]

Answer:

C. Blinking vertical line on your screen

Explanation:

A cursor is tha blinking vertical line on your screen.

5 0
3 years ago
Outlook 2016 is not only an email client, but it can also be used to schedule meetings and
klio [65]

Answer:

Outlook 2016 is not only an email client, but it can also be used to schedule meetings and <u>Manage appointments</u>.

Explanation:

Microsoft Outlook is a tool that is used to send and receive emails. In outlook 2016 version it has many other advantages such as:

  1. Managing Contact Directory
  2. Managing Address Books
  3. Scheduling Meetings
  4. Managing Appointments
5 0
3 years ago
Read 2 more answers
Roger is part of a team that is responsible for employing a new information system, the design of which was developed using cust
antoniya [11.8K]

Answer:

A line with a diamond on one end

Explanation:

A line with a diamond on one end is used as a symbol for condition.

In a structured programming chart, condition represents the fact that one program module (a control module) determines subordinate modules that will be invoked.

5 0
3 years ago
How does defragmentation improve performance?
KIM [24]

Answer:

Regularly running the Disk Defragmenter utility improves system performance. 

5 0
2 years ago
Other questions:
  • Show what this program prints. Be exact and complete. Can you explain the behavior of each print statement? 1 2 3 4 5 6 7 public
    12·1 answer
  • Using your own computer, give the name of the operating system that runs it and give examples of five legal file names. Then cre
    13·1 answer
  • Which process is used to protect transmitted data in a vpn?
    12·1 answer
  • Write a setInterval() function that increases the count by 1 and displays the new count in counterElement every 300 milliseconds
    12·1 answer
  • A label is any word that appears in a cell of a spreadsheet.<br> True<br> False
    5·2 answers
  • Using JavaScript, how does a web page react when the user enters a wrong email ID in a registration form?
    9·1 answer
  • How do I give Brainliest? Will give brainliest when I figure out how
    8·2 answers
  • Write an application that inputs a five digit integer (The number must be entered only as ONE input) and separates the number in
    10·1 answer
  • Which of the following statements is TRUE of a peer-to-peer network?
    10·1 answer
  • What is output by the following line of code?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!