(Guess-the-Number Game Write a program that plays the game of\"guess the number\
ID: 3793214 • 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 quess.- The player then types a first guess. The program responds with one of the following: 1. Excellent! You guessed the number!DWould you like to play again (y or n)? 2. Too low. Try again. 3. Too high. Try again. If the player's guess is incorrect, your program should loop until the finally gets player 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. Note: you need to have more than one file in this lab.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand (time(NULL));
int max = 1000;
int min = 1;
int guess;
string choice = "yes";
cout<<"I have a number between "<<min<<" and "<<max<<endl;
cout<<"Can you guess my number? "<<endl;
while(choice == "yes"){
int randNum = rand() % 1000 + 1;
cout<<"Please enter yoour guess: ";
cin >> guess;
while(guess != randNum){
if(guess < randNum){
cout<<"Too low. Try again."<<endl;
}
else{
cout<<"Too high. Try again."<<endl;
}
cout<<"Please enter yoour guess: ";
cin >> guess;
}
cout<<"Excellent. You guessed the number. Would like to play again (yes or no): ";
cin >> choice;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
I have a number between 1 and 1000
Can you guess my number?
Please enter yoour guess: 500
Too high. Try again.
Please enter yoour guess: 300
Too low. Try again.
Please enter yoour guess: 400
Too high. Try again.
Please enter yoour guess: 380
Too high. Try again.
Please enter yoour guess: 360
Too high. Try again.
Please enter yoour guess: 350
Too high. Try again.
Please enter yoour guess: 330
Too low. Try again.
Please enter yoour guess: 340
Too low. Try again.
Please enter yoour guess: 345
Too low. Try again.
Please enter yoour guess: 348
Too high. Try again.
Please enter yoour guess: 347
Excellent. You guessed the number. Would like to play again (yes or no): no
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.