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
Computer in country development explain explain explain in presentation.<br>​
yaroslaw [1]

Answer:

A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as programs. These programs enable computers to perform a wide range of tasks. A computer system is a "complete" computer that includes the hardware, operating system (main software), and peripheral equipment needed and used for "full" operation. This term may also refer to a group of computers that are linked and function together, such as a computer network or computer cluster

6 0
2 years ago
You have recently been called to troubleshoot network connectivity problems at a user's workstation. You have found that the net
Verdich [7]

Answer:

The answer is "Pass the cable into the ceiling instead of over the floor".

Explanation:

Network access explains the complex process of link different parts of the network with each other, e.g. while using switches, routers, and access points, and whether, that system works.

  • To replace the cable with a pair cable graded in plenum, covered, twisted.
  • We use the cable to pass through the ceiling rather than through the concrete, eliminating the issue and stopping it from occurring again.
3 0
3 years ago
The input to the following algorithm is a positive integer. The goal is to find the digit in the hundreds place of the integer.
Serggg [28]
The first division should reduce the hundreds digit to the units digit by dividing by 100.  Ignoring remainder means ignoring the previous units and tens digits.
The second division, where we keep the remainder is to extract the units digit by ignoring the quotient.  So we divide by 10.  The discarded digits are the tens and higher digits.

4 0
2 years ago
Read 2 more answers
In Excel, ____ is/are used to place worksheet, column, and row titles on a worksheet.
vladimir1956 [14]
In excel, [Text] is used to place worksheet, column and row titles on a worksheet.
In excel you can inset text to make comment, to input words or number and to make title. 
8 0
2 years ago
Intelligent computer uses _________ to learn.
Olenka [21]

Answer: a test

Explanation:

5 0
2 years ago
Other questions:
  • Creating calendar events prevents individuals from being able to schedule a time to collaborate.
    11·1 answer
  • You select a database or change to a different database with the ____ function.
    10·1 answer
  • Charles Mott works for a company called VeriSign that acts a trusted third party to verify information. One of Charles' largest
    15·1 answer
  • Do people answer questions more on this site or be on social more ??? no right or wrong answer your opinion
    6·1 answer
  • 1. Explain the difference between a web browser and a search engine. (2 points)
    8·1 answer
  • PLZ ANSWER WORTH 20 POINTS!! URGENT!
    7·2 answers
  • Question 1
    8·1 answer
  • Avi does not want to save his internet browsing details on his computer. What should he do?
    11·1 answer
  • C
    5·1 answer
  • What are tasks performed by pascaline?​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!