Random number generation

This is a discussion on Random number generation within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a piece of code using a random number generation result. I have not been able ...

WHOA!

You made this way too complex. First off, it doesnt do it quite corectly. I runs it 20 times regardless if the guess it right or not. Here, I wil hep you by giving you some usefull code. It may not all be correct in the random part, because i am not changing that. What compiler are you using?

// 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;
}

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens