Hello all! Well, I'm attempting to generate a random number and make it equal to the variable "random_number". After the program makes a random number, the user will be prompted to guess what the number is, and based upon what they entered, it will tell them if they have guessed correctly, to high, or to low. Here's what I have so far:
#include <iostream>
usingnamespace std;
int user_guess, random_number;
int rand ( void );
int main()
{
random_number = rand();
cout << "A sale has occurred. Please enter payment." << endl;
cin >> user_guess;
if (user_guess == random_number)
cout << "You have entered the exact amount. You're awesome!" << endl;
elseif(user_guess < random_number)
cout << "You have underpayed. Please enter again." << endl;
else
cout << "You have overpayed. Please enter again." << endl;
return 0;
}
Right now, I'm getting the error: "undefined reference to 'rand()'". There's more to this that I need to do (if over payed, need to spit back change, in coins / dollars, among others) but if you'd be so kind as to clarify how this random number generator works, that'd be great.
You need to #include <cstdlib>
Also read http://cplusplus.com/reference/clibrary/cstdlib/rand/ .
You will have to call srand once in your program, otherwise rand() will always generate the same sequence of numbers.