I poked around with a different, lazier version of the number guessing game which does a lot of checking.
Code:
#include <iostream.h>
#include <string.h>
#include <math.h>
int main(){
int counter=1, number=1, lasttry=1, looplimit=0;
double increment=50, step=25;
char choice[50];
cout << "Input a number (1-100): ";
cin >> number;
while(increment!=number){
lasttry=increment;
if(increment>100||increment<1||looplimit>4){
cout << "You cheated, you gotta tell me the truth son!\n";
return 0;
}
cout << "You say the number isn't " << increment << ", so is it [H]igher or [L]ower than it? ";
cin >> choice;
if(choice[0]=='h' || choice[0]=='H'){
increment+=step;
if(lasttry==increment){
increment++;
looplimit++;
}
step/=2;
step=floor(step);
counter++;
}
else{
increment-=step;
if(lasttry==increment){
increment--;
looplimit++;
}
step/=2;
step=floor(step);
counter++;
}
}
if(number==50){
cout << "The computer instantly guessed that your number was 50.\n";
return 0;
}
cout << "I guess the number you thought about is " << increment << ", and I WIN!\n";
cout << "It took the computer a total of " << counter << " tries to guess your number.\n";
return 0;
}
"Stephen Wolfram is the creator of Mathematica and is widely regarded as the most important innovator in scientific and technical computing today." - Stephen Wolfram