Random number guesser

I don't understand my error, I get 2 every time. I coded this in C++ obviously. If someone could please help me that would be great, any help would be great.

source code: ---------------

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int iG;
int uN;
iG = rand() % 10 + 1;
cout << "Enter a number, 1-10 to see if I can guess it." << endl;
cin >> uN;


if(uN<1){
cout << "The number you entered is less than 1, make sure it is 1-10." << endl;
cout << "Enter anything and ENTER to exit the program." << endl;
int q;
return 0;
}
if(uN>10){
cout << "The number you entered is over 10, 1-10!" << endl;
cout << "Enter anything and ENTER to exit the program." << endl;
int q;
return 0;
}
if(uN==iG){
cout << "Yes! I guessed your number! It is" << uN << endl;
cout << "Enter anything and ENTER to exit the program." << endl;
int q;
return 0;
}
if(uN!=iG){
cout << "Aww! I didn't guess correctly. I guessed "<< iG << "and you guessed " << uN << endl;
cout << "Enter anything and ENTER to exit the program." << endl;
int q;
return 0;
}
}
#include <ctime> and put the srand(time(NULL)) anywhere in your code to get different random numbers when using the rand() method. Without putting srand(time(NULL)), rand() will always generate the same random number. Hope this helps
Last edited on
thank you, I put in the srand(time(NULL)) but I was like.. what do I need to include, time isn't declared?

much appreciated my friend
As mentioned:
- #include <ctime> to use the time.
- Write srand(time(NULL)) anywhere to get static random generator based on time.

This will allow rand() to generate different random numbers, not same.
Have a look at the random class: http://www.cplusplus.com/reference/random/
Topic archived. No new replies allowed.