int main()
{
int secretNumber;
cout << "Type your number here.";
cin >> secretNumber;
int guess;
do
{
srand(static_cast<unsigned int>(time(0)));
int guess = rand();
cout << "Is it " << guess << endl;
if (guess < secretNumber)
{
cout << "\n\nOk I have to guess lower...";
}
else if (guess > secretNumber)
{
cout << "\n\nOK I have to guess Higher...";
}
else
{
cout << "\n\nYes I got it!";
}
}while (guess == secretNumber);
system ("pause");
return 0;
}
Basically, I am making a program where the computer tries to guess the number that I type in. There are no compiler errors, but when I run the program, It does not stop and only guesses the same number.
for example: Is it 34567. OK I have to guess lower... (and so on)
Basically it keeps repeating what it has already done. Is there a solution to this?
You're using it every time in the loop, and each time it sets the sequence of nubmers that rand will give you back to the start, so you get the same number each time you call rand.
Ya... I dont understand my code fully. Im trying to do one of the exercises in my book an so i am experimenting. Are you referring to the part with the srand? Because I took that out of the loop and put it under int guess;
Sorry for wasting your time. I just figure out why it was repeating. I had to take out int guess = rand(); of the loop as well. thank you for helping me figure this out.