我有一个作业要编写一个猜测我的数字的程序,但我必须在没有数字输入的情况下完成它。只有是/否的问题。到目前为止,这是我的代码,但它无法正常工作。例如,它无法猜出 24,50 和 75 的数字。它可以在输入数字的情况下正常工作和运行,但是如果没有它就无法弄清楚如何做到这一点,所以请给我一些提示:)
#include <iostream>
using namespace std;
int main()
{
cout << "Think of a number 1-100!!\n";
bool l=false;
int min=0;
int max=100;
int ind;
int h;
char answer;
int tries=0;
do
{
ind=(min+max)/2;
cout << "Is your number bigger than " << ind << "? (y/n): "; cin >> answer;
if(answer=='y')
{
h=ind+(ind/2);
}
else
{
h=ind-(ind/2);
}
tries++;
if(ind>h)
{
max=ind-1;
//cout << "ind: " << ind << endl;
//cout << "h: " << h << endl;
}
else if(ind<h)
{
min=ind+1;
//cout << "ind: " << ind << endl;
//cout << "h: " << h << endl;
}
else if(ind=h)
{
l=true;
//cout << "ind: " << ind << endl;
//cout << "h: " << h << endl;
cout << h << " is your number!\nWow I guessed it in " << tries << " tries!\nPat me!";
}
}while(!l && min<=max);
return 0;
}