(Guess-the-Number Game) Write a program that plays the game of \"guess the numbe
ID: 3792663 • Letter: #
Question
(Guess-the-Number Game) Write a program that plays the game of "guess the number" as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following. I have a number between 1 and 1000. Can you guess my number? Please type your first guess. The player then types a first guess. The program responds with one of the following: 1. Excellent! You guessed the number! Would you like to play again (y or n)? 2. Too low. 3. Too high. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player "zero in" on the correct answer.Explanation / Answer
#include<stdio.h >
using namespace std;
int main()
{
int r = (rand() %1001)+1;//generating random number between //1 and 1000
int n;
cout<<"I have a number between 1 and 1000 "<<endl;
cout<<"can you guess my number? "<<endl;
cout<<"Please type your first guess:" ;
cin>>n;//taking user input
while (n! =r) //validating loop, if matched then stops
{
if(n>r) cout<<"Too high. Try again :";//giving suggestions
else cout<<"Too low. Try again :";
cin>>n;
}
char c;
cout<<"Excellent! You have guessed the correct number, would you like to play again (y or n) :";
cin>>c;
if (c=='y') main() ;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.