The program does nothing when the user inputs "h" "l" and "y"
while creating this program these conditions should be followed;
Create a program that asks the user to think of a number. The program will attempt to guess the number and ask for input from the user.
Based on that response, the program will attempt to narrow down the answer using a ‘binary search’ algorithm, until the guess is correct or the user chooses to end the program early.
1. Create a new, default console project named ‘NumberGuess’.
2. Change the default ‘hello world’ code to output the message “Think of a number between 1 and 100”.
3. Declare three integer variables: highest, lowest, and attempts.
4. Initialize highest to 100 and the others to zero.
5. Increment the number of attempts.
6. Declare an integer, ‘guess’. Initialize to the value returned by the formula lowest + ((highest - lowest) * 0.5)
7. Display the message “I guess {n}. Am I right?” (Display the value of guess instead of {n}) and on the next line: “’q’ to quit, ‘y’ if correct, ‘h’ if too high, ‘l’ if too low.”
8. Using an if,else if,else blocks as needed, implement the remaining steps.
9. If the user’s response is ‘Y’, display a message “I guessed it in {n} attempts” and cease all further attempts at guessing.
10. If the user’s response is ‘H’, reduce the highest possible guess limit to the current guess.
11. If the user’s response is ‘L’, increase the lowest possible guess limit to the current guess.
12. If the user’s response is ‘Q’, display a message ‘You quit. Bye.’ and cease all further attempts at guessing.
13. If the user makes any other response, display a message “I didn’t understand that response.”
14. Repeat the process of guessing from step 5 above.
15. When all attempts at guessing have ceased, for whatever reason, exit the program with an error code equal to the number of attempts.
When done, the program’s output should look something like this:
```
Think of a number between 1 and 100
I guess 50. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too
low.
h
I guess 25. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
h
I guess 12. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 18. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 21. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 23. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
h
I guess 22. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
y
I guessed it in 7 attempts..
my code;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Think of a number between 1 amd 100" << endl;
int highest = 100;
int lowest = 0;
int attempts = 0;
attempts++;
int guess = lowest + ((highest - lowest) * 0.5);
string userinput;
cout << "I guess " << guess << ". Am I right?" << endl;
cout << "'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low." << endl;
getline (cin, userinput);
while ( userinput != "y")
{
if (userinput == "h")
{
highest--;
}
else if (userinput == "l")
{
lowest++;
}
else if (userinput == "q")
{
cout << "You quit. Bye." << endl;\
}
else if (userinput == "y")
{
cout << "I guessed it in " << attempts << " attempts" << endl;
}
else
{
cout << "I didn’t understand that response." << endl;
}
}
}
| |