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
BlackZzzverrR [31]
3 years ago
15

1. Write a program that plays the game of "Guess the Number" as follows: Your program should choose (generate a random number) t

o be guessed by the user in the range of 1 to 1000. The player will type a guess. The program responds with one of the following: a) Excellent! You guessed the number! Would you like to play again (y or n)? b) Too Low. Try Again. c) Too High. Try Again.
Computers and Technology
1 answer:
kirill115 [55]3 years ago
8 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 play again (y or n)? " <<endl;

cin>>reply;  

if(reply == 'y')

    game();      

if(reply == 'n')

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

return 0;

}

void game()

{

   int num, guess;

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

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

   do

{      

        do

        {

            cout<<"Guess any number between 1 and 1000. "<< endl;

            cin>>guess;

            if(guess<1 || guess>1000)

            {

                cout<<"Invalid number. Guess any number between 1 and 1000. "<<endl;

                cin>>guess;

            }        

             

        }while(guess<1 || guess>1000);    

     

    if(guess < num)

    {

        cout<<"Too Low. Try again."<<endl;            

    }      

    if(guess > num)

    {

        cout<<"Too High. Try again."<<endl;              

    }      

}while(guess != num);  

cout<<"Excellent! You guessed the number!"<<endl;

}

 

OUTPUT

Welcome to the game of guessing the correct number  

Guess any number between 1 and 1000.  

45

Too Low. Try again.

Guess any number between 1 and 1000.  

56

Too Low. Try again.

Guess any number between 1 and 1000.  

63

Too High. Try again.

Guess any number between 1 and 1000.  

61

Too High. Try again.

Guess any number between 1 and 1000.  

57

Too Low. Try again.

Guess any number between 1 and 1000.  

58

Too Low. Try again.

Guess any number between 1 and 1000.  

59

Too Low. Try again.

Guess any number between 1 and 1000.  

60

Excellent! You guessed the number!

Would you like to play again (y or n)?  

n

Quitting the game...  

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 executes until a valid input is received from the user.

     do

        {

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

            cin>>guess;

            if(guess<5 || guess>15)

            {

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

                cin>>guess;

            }              

            attempt++;              

        }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;

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;  

    }  

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
Which of the following common software packages would help a business
Jobisdone [24]

Answer:

D. Spreadsheets

Explanation:

While A also seems correct, a spreadsheet is best at collecting and organizing data. Databases would only store data while a spreadsheet would keep those records, as well as help calculate a budget or payroll.

3 0
3 years ago
What’s the name of the technology that lets ryzen™ 5 series cpus access the entire gddr memory of radeon™ rx gpus?.
amid [387]

The name of the technology that lets ryzen™ 5 series cpus access the entire gddr memory of radeon™ rx gpus is AMD Smart Access Memory.

<h3>What is AMD Smart Access Memory?</h3>

AMD Smart Access Memory is known to be a type of memory that helps AMD Ryzen processors to use all their  full power of the graphics card memory.

Conclusively, it is a memory that gives room for a person to combine a Radeon RX 6000 series GPU with the used of a Ryzen processor to make your gaming performance better.

Learn more about AMD Memory from

brainly.com/question/18846925

6 0
2 years ago
you need to configure a wireless network using wpa2-enterprise. which of the following components should be part of your design?
Iteru [2.4K]

Answer: AES encryption

802.1x

Explanation:

4 0
2 years ago
Which of these can expose a computer to a virus? Check all that apply.
Rina8888 [55]

Answer:

downloads, websites, emails?

4 0
3 years ago
Read 2 more answers
How much cell phone data does the average person use a month
azamat
It all depends on what you're doing online.
7 0
3 years ago
Other questions:
  • Which of the following is not an algorithm?
    8·1 answer
  • Which setting indents all but the first line of a paragraph by the selected length?
    6·1 answer
  • Which wireless device does the manager most likely have before being offered the upgrade by her supervisor?
    8·2 answers
  • Carol typed a memo to send to everyone in her department. To create this memo, she used a _____. spreadsheet word processor fax
    10·2 answers
  • __________%of the users have left websites in frustration due to poor navigation.
    13·1 answer
  • Nikolas has a idea that he could use the compressed carbon dioxide in a fire extinguisher to propel him on his skateboard. Nikol
    13·2 answers
  • On a digital clock the displayed time changes constantly (True or False)
    15·1 answer
  • How many bytes are there in 256 Kbytes?
    6·1 answer
  • This finding maximum number function will keep on going until we reach at the last value
    12·1 answer
  • An online bank wants you to create a program that shows prospective
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!