Please help with rand function

I need to design a program that generates 100 random numbers and keeps a count of how many of those random numbers are even and how many are odd. I do not know why I keep getting this error message "invalid operands of types `int' and `<unknown type>' to binary `operator<<' for line 17 and 19. Here is my code. Please please help. Thanks a lot

#include <iostream>
#include <string>

using namespace std;
int main()
{
int evenNum = 0;
int oddNum = 0;
int counter = 1;
int num = 0;


for (counter=1; counter<=100; counter++)
{
num = rand () % 1000;
if (num % 2==0)
{evenNum = evenNum + 1 << endl;}
else
{oddNum = oddNum+1 << endl;}
counter =counter + 1;
}
cout << "The count of even number is "<<evenNum << endl;
cout << "The count of odd number is "<<oddNum << endl;
system("pause");
return 0;
}
Last edited on
1
2
3
4
if (num % 2==0)
{evenNum = evenNum + 1 << endl;}
else
{oddNum = oddNum+1 << endl;}


What's with the endl's? Remove them.

Also, don't forget to seed the random number generator, by using std::srand(). Example here:

http://cplusplus.com/reference/cstdlib/srand/
Last edited on
Thank you very much for your help. You saved my life. Would you please explain more about the srand function? I understand that it acts like a seed for the generator. I want to know if time function is a default syntax for this srand? is there any other time to be used as seed for srand? I am very fresh to C++
I want to know if time function is a default syntax for this srand? is there any other time to be used as seed for srand?
srand() should be used only once in the program, and it should be fed a number which is random enough. Calling srand() with a random enough value will ensure that the values generated by rand() will be highly random too.

time() will return a number representing the current time -- the time when the program is run and the function is called. That's why time() is preferred, because for all practical purposes it returns a value that is random enough.

It's not anything to do with syntax that time() is used. That call to time(NULL) will become a value for srand().

I am very fresh to C++

I suggest you read the Tutorial.
http://cplusplus.com/doc/tutorial/
Last edited on
Thank You. You are awesome :)
Topic archived. No new replies allowed.