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 offers more reliable antivirus protection? Question 43 options: A) antivirus software on user PCs B) anti
marissa [1.9K]

The option which offers more reliable antivirus protection is an installation of: B) antivirus software on the mail server.

<h3>What is an antivirus?</h3>

An antivirus can be defined as a software application that is designed and developed to prevent, scan, detect, delete and protect a computer from viruses and malwares that are capable of infecting and destroying both system and user files.

In this context, the option which offers more reliable antivirus protection is an installation of antivirus software on the mail server, rather than on user PCs.

Read more on antivirus here: brainly.com/question/17209742

#SPJ1

5 0
2 years ago
Name 3 examples of operating system software that are not Windows based.
anyanavicka [17]
Ubuntu, Linux, and Mint
4 0
3 years ago
_____is used to organize and modify the properties of the current selection.
vova2212 [387]

Answer:

<h3>☘ QUESTION☘</h3>

_____is used to organize and modify the properties of the current selection.

<h3>☘ ANSWER☘</h3>

(B) Property Inspector

Explanation:

✨BY#NICHOLE✨

4 0
2 years ago
Imagine you are responsible for making a presentation that includes a representation of the logic flow through a process. You un
svetoff [14.1K]

Answer:

find reliable resources

Explanation:

if you find reliable resources, than you might able to have more help i know im not answering..but im trynna help

3 0
3 years ago
Agent Phil Coulson developed this program to register Avengers in S.H.I.E.L.D's database using cutting-edge programming language
rosijanka [135]

eh I think yes I think. Maybe

6 0
3 years ago
Other questions:
  • what properties are associated with all Microsoft Office files and include author, title, and subject
    5·1 answer
  • 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
    5·1 answer
  • 1 megabyte is equal to 1024 gigabyte. True/False​
    11·2 answers
  • How do you change the text style on brainly mobile ?
    12·1 answer
  • Write a program in C++ or C that includes two different enumeration types and has a significant number of operations using the e
    15·1 answer
  • Let's go! About to hit 40k!!! Glad to help on Brainly®!!!
    11·1 answer
  • What does advance mean​
    6·1 answer
  • Which 1947 Invention paved the way for the Digital Revolution?
    12·2 answers
  • Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input
    5·1 answer
  • I WILL MARK BRAINLEST FOR THIS!!!
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!