Hey! New to the forums here and new to C++ in general. I am trying to make a random number game and I need some help. :\ I need to make the random numbers be between 1 and 20, not just completely random numbers. Here is my code.
//using the std namespace for easier writing
using namespace std;
//initializing the main program
int main()
//main code
{
//display weclome message
cout << "Hello, and welcome number adding system!\n\n";
//press any key to continue and skip a line
cin.get( );
cout << endl;
//loop the random number
int loop_int = 0;
while(loop_int < 1)
{
int rand_int = rand();
cout << rand_int << endl;
If you want to have random numbers between a specific range you can use rand_value = (rand() % range) + offset.
For your example range would be 20 and offset 1 (the rand()%range produce numbers from 0 to range-1).
You will find out that the random numbers are always the same when you run your program.For that reason you have to use the srand to set a really random start point. Here is the documentation for srand from microsoft: http://msdn.microsoft.com/en-us/library/aa272944(VS.60).aspx
I entered that piece of code but I get an error when I try to compile:
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\consoleproject\consoleproject\consoleproject.cpp(22) : error C2296: '%' : illegal, left operand has type 'int (__cdecl *)(void)' Any suggestions?