Code:
// Guess - Number Game
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int guessCounter, x, guess;
// Only get a single random number, otherwise you are getting
// a new one each time you go through the loop. Also, you need
// to seed the random number generator otherwise you get the
// same "random" number each time you run the program. Last,
// what you had will only give 1 as the number, try picking something
// larger to do the "%" with, for example, this will give a number
// from 1 to 100.
srand( time(NULL) );
x = 1 + rand() % 100;
cout <<"I've got the number. Please guess it:";
for( guessCounter = 0; guessCounter < 20; guessCounter++ )
{
cin >> guess;
if (guess < x)
cout << "Your guess is less than the number. Please guess again:";
else if (guess > x)
cout << "Your guess is greater than the number. Please guess again:";
else if (guess == x)
{
cout << "You guessed it!"<<endl;
break;
}
}
return 0;
}