I am having a issue with rand(), I only really half way through the tutorial so sorry if this is a obvious issue but was a bit bored so figured I would try to reinforce some of the things I am learning.
The number being generated is always 2 this is my code:
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
usingnamespace std;
int main ()
{
int random = rand() % 10 + 1;
int guess;
do {
cout << "Guess the number between 1-10:" << endl;
cin >> guess;
if (guess == random)
cout << "Congratulations! You guessed correct." << endl;
else
cout << "Sorry incorrect.\n" << endl;
} while (guess != random);
system("PAUSE");
}
The odd thing is when I made a test script and used this
1 2 3 4
random = rand() % 10 + 1;
cout << random;
random = rand() % 10 + 1;
cout << random;
and repeated it, it would always generate a different number?
Also just as a side question is there anything wrong with using only endl and not \n I tend to prefer to use endl as it seems cleaner to read but I feel obliged to use \n.