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 guess.
The player then types a first guess. The program responds with one of the following:
1. Excellent! You guessed the number!
Would 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 player finally gets 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.


```
// Randomly generate numbers between 1 and 1000 for user to guess.
#include
#include
#include
using namespace std;

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

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

// guessGame generates numbers between 1 and 1000 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 1000
// 1 is shift, 1000 is scaling factor
answer = 1 + rand() % 1000;

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

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

// prompt for another game
cout << "\nExcellent! You guessed the number!\n"
<< "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.\n? ";
else
cout << "Too high. Try again.\n? ";

return false;
} // end function isCorrect
```
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
? 500
Too high. Try again.
? 250
Too low. Try again.
? 375
Too low. Try again.
? 438
Too high. Try again.
? 405
Too high. Try again.
? 380
Too low. Try again.
? 393
Too low. Try again.
? 399
Too high. Try again.
? 396
Too low. Try again.
? 398
Too high. Try again.
? 397
Excellent! You guessed the number!
Would you like to play again (y or n)? n

Computer Science & Information Technology

You might also like to view...

Answer the following statements true (T) or false (F)

1. In a general sense, a class is a collection of statements that performs a specific task. 2. If a specific task is performed in several places in a program, a method can be written once to perform that task and then be executed any time it is needed. 3. Every method must have a nonempty parameter list. 4. A method definition has two parts: a header and a body. 5. The statements that make up the method body are enclosed inside a set of curly braces.

Computer Science & Information Technology

In order to view a webpage on any type of website, a device needs to have a web _____ installed.?

A. ?browser B. ?client C. ?manager D. ?server

Computer Science & Information Technology

Case 2 ? Alex, an artist, has made a bet. He bet his friend, Blaine, that he could create a digital painting that Blaine wouldn't be able to differentiate from a digital scan of a real painting. Blaine is skeptical, because he knows that a real painting contains certain brushstrokes that he doesn't believe can be duplicated digitally. Alex knows that he needs to be able to create a stroke that contains multiple colors, the same way a real brush might. What tool should he use?

A. Fade B. Color Mixer C. Mixer Brush D. Smudge

Computer Science & Information Technology

The 2001 _________ is the first international treaty seeking to address Internet crimes by harmonizing national laws, improving investigative techniques, and increasing cooperation among nations.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology