Random Number Generator not working?

Well, I'm a beginner programmer, and I would like to program games. So I made this game where the user would have to guess the number the computer generated. But I wanted to reverse it. I got two problems one, everytime it generates a number its the same number. The only time it changes is when i close the window and run it again.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
srand(time(0));

int guess = rand() % 10 + 1;
int tries = 0;

cout << "\tGuess my Number\n\n";
cout << "Think of a Number between 1 - 10.\n";

char answer ;
do
{
cout << "Is you number " << guess << "<Y/N>\n";
cin >> answer;
++tries;

} while (answer == 'N' || answer == 'n');

cout << "\nThat's it I got it in , " << tries << " guesses!\n";

system ("pause");
return 0;

}
Last edited on
[code] "Your code goes here" [/code]
The number is always the same because you aren't changing it. Inside the do-while loop you aren't touching guess.
Also don't use system
_ http://www.cplusplus.com/forum/beginner/1988/
_ http://www.cplusplus.com/forum/articles/11153/
Look at your loop:

1
2
3
4
5
6
7
do
{
cout << "Is you number " << guess << "<Y/N>\n";
cin >> answer;
++tries;

} while (answer == 'N' || answer == 'n');


This is the code that's repeating.

Notice that you never change 'guess' anywhere in that code. That is why the computer keeps making the same guess over and over.
ok well, i didn't know how to change guess, so I put the
int guess = rand() % 10 + 1; in the loop and it works but I don't know it that was the right move.
Last edited on
Topic archived. No new replies allowed.