Write a C++ program to implement the Number Guessing Game. In this game, the com
ID: 3827113 • Letter: W
Question
Write a C++ program to implement the Number Guessing Game. In this game, the computer chooses a random number between 1 and 100. The player tries to guess the number in 7 or less attempts. Each time the player enters a guess, the computer will display a HINT to either guess HIGHER or LOWER ifthey do not guess the random number. Once the player guesses the number, congratulate the player and prompt the user ifthey would like to play the game again. suggested Pseudocode (You may design the program yourself or you can use this Pseudocode. You do NOT have to use functions Initialize variables for the randomnumber, the guessed number, the number of attempts, and the repeat option dof Set attempts equal to 0 in case player wants to play game again Randomly generate a number between 1 and 100 (srand0 and rand0) Display "Guess My Number Game" While the "guessed number does not equal the random number AND all 7 attempts have not been used Prompt the user to enter a number between 1 and 100 Add one to the number of attempts if (guessed number is greater than the random number) Display a Hint to Guess LowER else if (guessed number is less than the random number) Display a Hint to Guess HIGHER else Display Congratulations, you guessed the (random number in how many attempts were use if the guessed number does not equal the random number Display Sorry, you did not guess the number Ask the player if they would like to play the game again? hwhile the player wants to play the game, go to the beginning of the loop to play againExplanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
char chr;
int main()
{
int number;
int guess;
int tries;
char answer;
srand(number>0);
do
{
number=rand()%100+1;;
cout<<"Enter a number between 1 and 50"<<endl;
cin>>guess;
if (number<guess)
cout<<"Too high try again"<<endl;
tries=1;
} while(number>guess);
cout<<"Too low try again"<<endl;
tries++;
if(number==guess)
cout<<"Congratualtions!! "<<endl;
cout<<"You got the right number in "<<tries<<" tries";
do
{
cout<<"Would you like to play again? Enter Y/N";
cin>>answer;
if ('N')
cout<<"Thanks for playing!";
}
while(answer='Y');
cin>>chr;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.