Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to implement the Number Guessing Game . In this game, the co

ID: 3719760 • 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 with 7 attempts or less. Each time the player enters a guess, the computer will display a HINT to either guess 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 not guess the number after 7 attempts, tell the user “sorry” and reveal the random number to the user.

Suggested Pseudocode (You could use if you want)

Initialize variables for the random number, the guessed number, the number of attempts, and the repeat option

do

{

   Set attempts equal to 0 in case player wants to play game again

    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 all 7 attempts have not been used

   {  

      Prompt the user to enter a number between 1 and 100 and retrieve the number from user

      Add one to the number of attempts

      if (guessed number is equal to the random number)

          Display Congratulations, you guessed the (random number) in (how many attempts were used)

      else if (the number of attempts is equal to 7)

          Display Sorry you did not guess the number and what the (random number) 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 the beginning of the loop to play again

Explanation / Answer

Please find my working code.

#include <iostream>
#include <cstdlib> //header file needed to use srand and rand
#include <ctime> //header file needed to use time

using namespace std;

int main()
{
unsigned seed; //Random generator seed
double number, userGuess;
int attempts = 0;

seed = time(0); //Use of the time function to get a 'seed' value for srand
srand (seed);
number = rand() % 100 + 1; //Limiting the range of the random number

//cout << number << endl; //comment this line to hide the number random number.

do
{
cout << "Guess what the number is?: ";
cin >> userGuess;
attempts++;

if (userGuess > 100 || userGuess < 1)
cout << "The number is in between 1 and 100" << endl;
else if (userGuess > number)
cout << "Too High!! Try Again." << endl;
else if (userGuess < number)
cout << "Too Low!! Try Again." << endl;
else if (userGuess == number)
cout << "Correct!! It took you " << attempts << " tries." << endl;
} while (userGuess != number);


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote