// G. Hagopian--PPP4 Exercise 4
/*
4. Write a program to play a numbers guessing game. The user thinks of a
number between 1 and 100 and your program asks questions to figure out what
the number is (e.g., “Is the number you are thinking of less than 50?”).
Your program should be able to identify the number after asking
no more than seven questions. Hint: Use the < and <= operators and
the if-else construct.
*/
#include
using namespace std;
int main() {
int smallest = 1, largest = 1000;
bool found = false;
char answer{};
while (!found) {
cout << "Is your number greater than " << (smallest + largest) / 2 << "?\n";
do {
cout << "Enter 'y' for yes or 'n' for no: ";
cin >> answer;
} while (tolower(answer) != 'n' && tolower(answer) != 'y');
if (answer == 'y') {
smallest = (smallest + largest) / 2;
}
else largest = (smallest + largest) / 2;
if (largest - smallest <= 2) found = true;
}
cout << "Was your number " << largest << "?";// -(largest - smallest) / 2 << "?";
if ((largest + smallest) % 2 == 1) cout << "Your number was " << smallest;
else cout << "\nYour number was " << smallest+1;
}