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

I need help writing this program Write a program that will ask the user to selec

ID: 3623897 • Letter: I

Question

I need help writing this program


Write a program that will ask the user to select an integer number in the range 1 – 100 (inclusive). Store this value in the int variable value.

Prompt the user to guess the random number. Tell the user that the number is in the range 1 – 100.

Store the user input in the variable guess.

While (guess does not equal value)

Prompt the user to guess the random number. Tell the user that the

number is in the range 1 – 100.

Store the user input in the variable guess.

End loop

Tell the user their guess was correct and thank them for using your program.

Exit your program with a return statement

Explanation / Answer

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstdlib>
using std::rand;

#include <ctime>

void guessGame(); // function prototype
bool isCorrect( int, int ); // function prototype

int main()
{
   // srand( time( 0 ) ); // seed random number generator
   guessGame();

   return 0; // indicate successful termination

} // end main

// guessGame generates numbers between 1 and 100
// and checks user's guess
void guessGame()
{
   int answer; // randomly generated number
   int guess; // user's guess
   char response; // 'y' or 'n' response to continue game

   // loop until user types 'n' to quit game
   do {
      // generate random number between 1 and 100
      // 1 is shift, 100 is scaling factor
      answer = 1 + rand() % 100;

      // prompt for guess
      cout << "I have a number between 1 and 100. "
           << "Can you guess my number? "
           << "Please type your first guess." << endl << "? ";
      cin >> guess;

      // loop until correct number
      while ( !isCorrect( guess, answer ) )
         cin >> guess;     

      // prompt for another game
      cout << " Excellent! You guessed the number! "
           << "Would you like to play again (y or n)? ";
      cin >> response;

      cout << endl;
   } while ( response == 'y' );
} // end function guessGame

// isCorrect returns true if g equals a
// if g does not equal a, displays hint
bool isCorrect( int g, int a )
{
   // guess is correct
   if ( g == a )
      return true;

   // guess is incorrect; display hint
   if ( g < a )
      cout << "Too low. Try again. ? ";
   else
      cout << "Too high. Try again. ? ";

   return false;
} // end function isCorrect