#include<iostream>
int main()
{
std::cout<<"Let pc to guess.\n";
int guess, upper=100, lower=1;
char judge;
while(true)
{
guess=(upper+lower)/2;
std::cout<<guess<<"?(y/n) ";
std::cin.get(judge);
if(judge=='y'|| judge=='Y')break;
if(judge!='n'&& judge!='N')std::cout<<"Assuming a wrong guess.\n";
while(std::cin.get()!='\n')continue; //get rid of input
std::cout<<"High(h) or Low(l)? ";
while(true)
{
std::cin.get(judge);
if(judge=='h'|| judge=='H'){upper=guess;break;}
elseif(judge=='l'||judge=='L'){lower=guess;break;}
else std::cout<<"Only h or l accepted, again plese:";
while(std::cin.get()!='\n')continue;
}
while(std::cin.get()!='\n')continue;
}
}
This is my try for the fifth problem in beginner exercises. The problem seems ok, but I found
it is really annoying to deal with the assumed "stupid users input". Any better ideas? I am a
newbie to C++, so any comments are welcomed:)
oldnewbie, I just keep std:: to remind me those are in std. BTW, how to keep the code area smaller like yours?
Mine seems too big.
matsom, thanks for the links.