The following program uses a recursive function to guess a number between 0 and 1000, asking at each iteration whether the guessed number is correct, too low or too high. You can download the source code.
void guess(int lower, int upper, int my_guess) { cout << "\nMy guess is "<< my_guess <<". Press 'g' for greater, 'l' for lower or 'e' for equal.\n"; char ans[2]; cin >> ans; if (strcmp(ans,"e")==0) exit(0); else if (strcmp(ans,"g")==0) lower = my_guess; else if (strcmp(ans,"l")==0) upper = my_guess; if (lower == upper) cout << "user is cheating.\n"; else guess (lower, upper, (lower + upper)/2); // call guess within itself. } int main() { guess(0, 1001, 500); }