Firstly : You shouldn't ask for sample code without any of your own code - that's likely why this is being downvoted.
Moving on, you should decide how you want your program to guess. For example, a bisection algorithm might be useful for you here. You'd need to set some initial code which differed, in order to bound the person's number.
Eg: (pseudo-code)
int guessLower=0;
int guessUpper=1000000;
int myGuess=10;
// Ask user to pick number
// Ask user if number is == guess
// If yes, finish.
// If the answer is higher than the guess, change guessLower to the value of guess, and change guess to halfway between guess and guessUpper.
// If the answer is lower, do the opposite (guessUpper = myGuess; myGuess = (guessLower + myGuess)/2;
// Repeat until finished
This (~)code won't work if the value the person makes up isn't within the limits you set - so consider asking first if it is higher than guessLower, and lower than guessUpper. If not, try moving the markers by some amount. More complicated algorithms for searching can be easily imagined, and you should be careful about the differences between integers and doubles etc.
On a side note, for comparing strings, you should look up using equals(...)
, eg input.equals("higher");
. Don't use the C++ ==
for comparing the values of strings, I anticipate this might be a problem you'll hit...