I tried to do it using binary search, but I don't know how to get it to continue asking questions to guess the number. And the exercise asked to use the if-else construct and < and <=. I'm not sure how to use < and <= for this with binary search.
// Osman Zakir
// 12 / 22 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 4
// Execise specifications:
/**
* 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 "../../std_lib_facilities.h"
int main()
{
char answer = ' ';
int min = 1, max = 100;
int middle = max / 2;
int computer_number = middle;
do
{
cout << "Is the number you are thinking of " << computer_number << "? (y(es)/n(o))\n";
cin >> answer;
cin.ignore();
if (answer == 'y' || answer == 'Y')
{
cout << "So I found the number you were thinking of!\n";
break;
}
elseif (answer == 'n' || answer == 'N')
{
cout << "Should I go higher or lower? (h(igher) or l(ower)\n";
cin >> answer;
cin.ignore();
if (answer == 'h' || answer == 'H')
{
min = middle + 1;
computer_number = (max + min) / 2;
continue;
}
elseif (answer == 'l' || answer == 'L')
{
max = middle - 1;
computer_number = (max + min) / 2;
continue;
}
}
}
while (answer == 'n' || answer == 'N');
keep_window_open();
}
// Osman Zakir
// 12 / 22 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 4
// Execise specifications:
/**
* 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 "../../std_lib_facilities.h"
int main()
{
char found_answer = ' ';
char high_low = ' ';
bool found = false;
int min = 1, max = 100;
int middle = max / 2;
int computer_number = middle;
do
{
cout << "Is the number you are thinking of " << computer_number << "? (y(es)/n(o))\n";
cin >> found_answer;
cin.ignore();
if (found_answer == 'y' || found_answer == 'Y')
{
cout << "So I found the number you were thinking of!\n";
found = true;
}
elseif (found_answer == 'n' || found_answer == 'N')
{
cout << "Should I go higher or lower? (h(igher) or l(ower)\n";
cin >> high_low;
cin.ignore();
if (high_low == 'h' || high_low == 'H')
{
min = middle + 1;
computer_number = (max + min) / 2;
}
elseif (high_low == 'l' || high_low == 'L')
{
max = middle - 1;
computer_number = (max + min) / 2;
}
}
}
while (found == false);
keep_window_open();
}
By the way, if anyone wants to see the code in the higher file, just let me know and I'll copy-paste it.
Your 'middle' variable never goes above 51, and the program guesses the same number repeatedly '75', which is because (100+51)/2 is 75 so your program won't be able to go above 75. This sounds like the same reason why you get stuck at 25 when you attempt to go lower. Hope I helped!