How do you write a C plus plus program that attempts to guess the number from 1 to 15 that the user has in mind in 4 tries?

Answer:
#include <iostream>

using namespace std;

int main()
{
string hint;
int min = 1, max = 15, k = 0;
cout << "Please pick a number between 1 and 15."
<< "I will try to guess it in 7 steps. " << endl;
cout << "Is it smaller than 7 ? (yes or no : (y/n)) : ";

while(cin >> hint)
{
if (hint == "n")
{
min = (max+min)/2; max = max;
}

else if (hint == "y")
{
max = (max+min)/2; min = min;
}
k++;
if (k == 4) break;
if (max == min) break;
cout << "Is it smaller than " << (min+max)/2 << " ? " << k << endl;
}

cout << "It is " << min << " :) " << endl;
return 0;
}
First answer by ID1319713543. Last edit by Besiktasli43. Contributor trust: 0 [recommend contributor recommended]. Question popularity: 1 [recommend question].