Menu

[Solved]Cpp Code Write C Program Implement Number Guessing Game Game Computer Chooses Random Numbe Q37214988

.cpp code

Write a C++ program to implement the Number Guessing Game. Inthis game, the computer chooses a
random number between 1 and 100. The player tries to guess thenumber in 7 attempts or less. Each time the
player enters a guess, the computer will display a HINT to eitherguess HIGHER or LOWER if they do not
guess the random number. Once the player guesses the number,congratulate the player and prompt the user
if they would like to play the game again. If the user does notguess the number after 7 attempts, tell the user
“sorry” and reveal the random number to the user.
a. When you have completed the game code, play the game until youwin. Take a “screen shot” using the
print screen button (press the prt sc key) of the console windowwith your guesses. Paste the screen shot
into a Word file saved as your first name.
b. Submit your game .cpp file and your Word file saved as yourfirst name.

Suggested Pseudocode (You may design the program yourself or youcan use the
Pseudocode below. There is a template at the end of theseinstructions for the
pseudocode below. You do NOT have to use functions.)
Initialize variables for the random number, the guessed number, thenumber of attempts, and the repeat option
do
{
Set attempts equal to 0 in case player wants to play gameagain
Set the seed to be the number of seconds since 1970 (srand( ))
Randomly generate a number between 1 and 100 (rand() )
Display “Guess the Number Game”
While the “guessed” number does not equal the random number AND all7 attempts have not been used
{
Prompt the user to enter a number between 1 and 100 and retrievethe number from user –>
Perform Input Validation, display an error message if an incorrectnumber is entered and keep
prompting the user for a value between 1 and 100 until a validnumber is entered.
Add one to the number of attempts
if (guessed number is equal to the random number)
Display Congratulations, you guessed the (random number) in (howmany attempts were used)
else if (the number of attempts is equal to 7)
Display Sorry you did not guess the number and what the (randomnumber) was
else if (guessed number is less than the random number)
Display a Hint to Guess HIGHER
else if (guessed number is greater than the random number)
Display a Hint to Guess LOWER
Ask the player if they would like to play the game again?
}
}while the player wants to keep playing the game, go to thebeginning of the loop to play again

//Template designed from the Pseudocode above
//NAME:
//Guess the Number Game
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
//Declare variables for the random number, the guessed number, andthe number of attempts
int ?????, ????, ????;;
bool repeat;
do
{
//Set number of attempts to 0
?????????;
//Set the seed to be the number of seconds since 1970
srand(time(0));
//Generate a unique random number between 100 and 1
??????????;
//Display the name of the game
cout << “Guess the Number Game” << endl <<endl;
while (????????)//SEE PSEUDOCODE IN INSTRUCTIONS TO COMPLETELOOP
{
//Perform input validation with an error message to ensure
// the user enters a number from 1 – 100
cout << “Guess a Number Between 1 and 100: ?”;
cin >> ??????;
//increment the number of attempts by 1
??????;
if (????????)
cout << ???????? << endl;
else if (????????)
cout << ????????? << endl;
else if (???????)
cout << ???????? << endl;
else if(??????????)
cout << ?????????? << endl;
}
cout << “nWould you like to play again (1 for Yes, 0 forNo): “;
cin >> repeat;
cout << endl << endl;
} while (repeat);
system(“pause”);
return 0;
}

SAMPLE OUTPUT: CAUsersKaren DesktopGuessNumber Debugl GuessNumber.exe Guess the Number Gam Guess a Number Between 1 and 100

SAMPLE OUTPUT: CAUsersKaren DesktopGuessNumber Debugl GuessNumber.exe Guess the Number Gam Guess a Number Between 1 and 100: ?65 HINT: Guess HIGHER! Guess a Number Between 1 and 100: ?75 HINT: Guess HIGHER Guess a Number Between 1 and 100: 85 HINT: Guess LOWER! Guess a Number Between 1 and 100: ?80 HINT: Guess HIGHER! Guess a Number Between 1 and 100: ?83 HINT: Guess LOWER! Guess a Number Between 1 and 100: ?82 CONGRATULATIONS, you guessed the number 82 in 6 attempts! Would you like to play again (1 for Yes, 0 for No): uess the Number Game uess a Number Between 1 and 100: ?25 INT: Guess HIGHER uess a Number Between 1 and 100: ?30 INT: Guess HIGHER uess a Number Between 1 and 100: ?35 INT: Guess HIGHER! uess a Number Between 1 and 100: ?40 INT: Guess HIGHER! uess a Number Between 1 and 100: ?45 INT: Guess HIGHER uess a Number Between 1 and 100: ?50 INT: Guess HIGHER uess a Number Between 1 and 100: ?55 ORRY, you did not guess the number he number was 84 lould you like to play again (1 for Yes, 0 for No): Show transcribed image text SAMPLE OUTPUT: CAUsersKaren DesktopGuessNumber Debugl GuessNumber.exe Guess the Number Gam Guess a Number Between 1 and 100: ?65 HINT: Guess HIGHER! Guess a Number Between 1 and 100: ?75 HINT: Guess HIGHER Guess a Number Between 1 and 100: 85 HINT: Guess LOWER! Guess a Number Between 1 and 100: ?80 HINT: Guess HIGHER! Guess a Number Between 1 and 100: ?83 HINT: Guess LOWER! Guess a Number Between 1 and 100: ?82 CONGRATULATIONS, you guessed the number 82 in 6 attempts! Would you like to play again (1 for Yes, 0 for No): uess the Number Game uess a Number Between 1 and 100: ?25 INT: Guess HIGHER uess a Number Between 1 and 100: ?30 INT: Guess HIGHER uess a Number Between 1 and 100: ?35 INT: Guess HIGHER! uess a Number Between 1 and 100: ?40 INT: Guess HIGHER! uess a Number Between 1 and 100: ?45 INT: Guess HIGHER uess a Number Between 1 and 100: ?50 INT: Guess HIGHER uess a Number Between 1 and 100: ?55 ORRY, you did not guess the number he number was 84 lould you like to play again (1 for Yes, 0 for No):

Expert Answer


Answer to .cpp code Write a C++ program to implement the Number Guessing Game. In this game, the computer chooses a random number … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *