Question: I need help with this C++ assignment. In this assignment you will write a program in that can figure out a number chosen by a
I need help with this C++ assignment. In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.
A sample run of the program might look like this:
Guess a number between 1 and 100.
Is it 50? (h/l/c): h
Is it 75? (h/l/c): h
Is it 87? (h/l/c): l
Is it 81? (h/l/c): c
Great! Do you want to play again? (y/n): y
Guess a number between 1 and 100.
Is it 50? (h/l/c): l
Is it 25? (h/l/c): h
Is it 37? (h/l/c): c
Great! Do you want to play again? (y/n): n
Notice that the user inputs the characters h, l, and c for higher, lower, and correct, respectively.
For the game to work best, every time your program makes a guess it should choose the midpoint of the remaining possible values. Consider the first example above, in which the user has chosen the number 81:
On the first guess, the possible values are 1 to 100. The midpoint is 50.
The user responds by saying higher
On the second guess the possible values are 51 to 100. The midpoint of 75.
The user responds by saying higher
On the third guess the possible values are 76 to 100. The midpoint is 87.
The user responds by saying lower
On the fourth guess the possible values are 76 to 86. The midpoint is 81.
The user responds correct
Note: Please write the program by using the below instructure;
Declare the variables you will need:
- number (int) (**our program starts by guessing the midpoint, 50)
- Low, high (int)
- response (char) - -this will store what users response
- A variable to store the players response to do you want to play again (y/n)?
- (char) -- this will be the condition inside the first while loop
- A variable to store whether our program is inside the guessing loop (bool) -- this will be the condition inside the second while loop. STARTS = true
int main() {
While playing == y
Send output, using cout: Guess a number between 1 and 100.
While guessing
- Send output, using cout: is it << number << (h,l,c)? << endl;
- Receive input, using cin, either a h,l,c char variable, which we will want to store as the response variable.
- Use If statements how our program responds to each answer scenario:
If response == h :
Update low
Calculate the next midpoint.
Update our number variable for the next guess.
If response == l
Update high
Calculate the next midpoint.
Update our number variable for the next guess.
If response == c
Update the number for a new game.
Leave guessing loop (Id recommend using a break statement).
Break;
Send output: do you want to play again (y/n)? (youll need to use cout and cin)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
