Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Created November 16, 2012 23:17
Show Gist options
  • Save kgleeson/4091808 to your computer and use it in GitHub Desktop.
Save kgleeson/4091808 to your computer and use it in GitHub Desktop.
Computer guess
#include <iostream>
using namespace std;
int main(){
int guess = 50;
int guess_attempts = 0;
int answer ;
int helper = guess / 2;
cout << "Enter your number: ";
cin >> answer;
while(guess != answer){
if(guess > answer){
guess -= helper;
}
else if(guess < answer){
guess += helper;
}
if(helper > 1){
helper /= 2;
}
cout << guess << endl;
guess_attempts++;
}
cout << "Guessed in " << guess_attempts << " attempts" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment