Write a program named, hiLo.cpp. This program is a simple number guessing game that allows the user to guess a number between 1 and 100, inclusive.
The game will start with the program automatically selecting a random, secret number for the user to attempt to guess. The textbook covers generating random numbers and demonstrates the seeding of the random number generator so that a different secret number is generated each time the user plays this game.
Each time the user makes a guess, the game will respond by stating that the guess was either out of range, too high, too low, or correct. If the guess was too high or too low, then the range of values for the next guess narrows and is displayed in the prompt for the next guess. Each prompt must include the number of the next guess (which does not change when the guess is out of range) and the range of values in which the next guess should be. When a guess is valid but not correct, the range will get smaller. The range NEVER gets wider. If the guessed value is out of range, an error message is printed and that guess is not counted as a valid guess.
At the end, once the secret number has been correctly guessed, the numbers of valid and out of range (or invalid) guesses are displayed. This final display telling the user that the number was correctly (and the counts) must be display outside the loop and after the loop, NOT inside the main loop. The main loop should iterate as long as the guess is not correct. Get the first guess before the loop and then only ONCE inside the loop, at the end of the loop body.
When your program is executing, the console should look EXACTLY like the example execution log in this file. In this example execution log, notice the following:
1 – Every prompt for a guess contains the number of that guess and the min and max values for the range of that guess.
2 – The guess number does not increase when the guess is out of range.
3 – The boundaries of the range only get closer together as guesses are made; the range NEVER widens.
4 – Once the secret number is guessed, the program states how many valid and how many out of range guesses were made.
5 – After one round of the game is done, the user is given the option to play another round.
Finally, each time the program is executed, a diffferent list of secret numbers gets chosen. This happens if you have correctly seed the random number generator ONCE per execution. The seeding of the generator should be done before any of the loops and only once per program execution.
Notes: Make sure you generate numbers in the proper range. Test your program to see that you generatedifferent numbers each time. This is accomplished by seeding the generator each time the program runs. The textbook has an example of doing this. Make sure your output is formatted like the examples given above.
Expert Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(void)
{
srand(time(NULL)); // To not have the same numbers over and over again.
while(true)
{
int secretnumber = rand() % 100 + 1; // System number is stored in here.
int min = 1;
int max = 100;
int guess, validGuess= 0, invalidGuess =0;
char again;
while(true)
{
cout << “Enter a guess between ” << min << ” and ” << max << “: “;
cin >> guess;
if(guess == secretnumber)
{
cout << “Correct Guessn”;
validGuess++;
break;
}
else if(guess < min || guess > max)
{
cout << “Invalid Guessn”;
invalidGuess++;
}
else if(guess < secretnumber)
{
cout << “Too Low!n”;
validGuess++;
min = guess;
}
else if(guess > secretnumber)
{
cout << “Too High!n”;
validGuess++;
max = guess;
}
}
cout << “Valid Guess: ” << validGuess << endl;
cout << “Invalid Guess: ” << invalidGuess << endl;
// Get user response.
cout << “Would you like to play again (Y/N)? “;
cin >> again;
// Check user’s input and run again or exit;
if(again == ‘n’ || again == ‘N’)
{
cout << “Thank you for playing!”;
break;
}
else
{
cout << “nnn”;
}
}
return 0;
}