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 statement best describes the problem statement below?
marta [7]

Answer:

B

Explanation:

This problem statement does not clearly define the issues.

7 0
2 years ago
A storyboard is an example of an implementation tool.<br><br> A.<br> True<br><br> B.<br> False
Phantasy [73]
A True A storyboard is hardware installed directly onto the motherboard or using a <span>Peripheral device</span>
4 0
2 years ago
Read 2 more answers
What is the purpose of the operating system's processor management function?
Sloan [31]
The purpose of the operating system's processor management function is that it allows you to run multiple programs on your computer simultaneously. 
5 0
2 years ago
I need more friends plz talk with me
Bad White [126]

Answer:

hello hru??

Explanation:

<h2><em><u>PLEASE</u></em><em><u> MARK</u></em><em><u> ME</u></em><em><u> BRAINLIEST</u></em><em><u> AND</u></em><em><u> FOLLOW</u></em><em><u> ME</u></em><em><u> LOTS</u></em><em><u> OF</u></em><em><u> LOVE</u></em><em><u> FROM</u></em><em><u> MY</u></em><em><u> HEART</u></em><em><u> AND</u></em><em><u> SOUL</u></em><em><u> DARLING</u></em><em><u> TEJASWINI</u></em><em><u> SINHA</u></em><em><u> HERE</u></em><em><u> ❤️</u></em></h2>
6 0
3 years ago
Read 2 more answers
A technician configures a switch with these commands:SwitchA(config)# interface vlan 1 SwitchA(config-if)# ip address 192.168.1.
yanalaym [24]

Answer:

SVI (Switched Virtual Interface)

Explanation:

For a switch to have an IP address, a switch virtual interface must be configured. This allows the switch to be managed remotely over the network.

5 0
3 years ago
Other questions:
  • In your opinion, is it more beneficial to have many folders or is it better to “nest” subfolders? Explain your response.
    10·2 answers
  • There are ways to perform computer commands quickly and multiple times. <br> a. True <br> b. False
    11·2 answers
  • Discuss what they need to consider if they wish to use their devices when they are away from home?
    6·1 answer
  • Juliet is trying to increase her savings account and she decides that she is going save an additional $700 every month. If her m
    9·1 answer
  • A _____ is the useful part of a transmission through a network.
    15·1 answer
  • How can software be used to protect against hardware failures in systems that embody both? Give two examples of how software can
    14·1 answer
  • In addition to using comments and track changes, you can also use the comparison feature for reviewing documents. In this activi
    11·1 answer
  • Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMile
    10·1 answer
  • Pls help me<br> first one to answer correctly gets brainly points
    6·2 answers
  • Kinda strange, and im good
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!