Vectors and Random Number Assign

1
2
3
4
5
6
vector<int> group1r;
vector<string> group1;
for(cnt=0;cnt<20;cnt++)
    { // Beggining of for loop   
         group1r.push_back ((rand() % group1.size())/2 + 1);
    }


Anyone know why this portion of code is making my program fail and in an unexpected error?
Last edited on
That looks valid to me.
If you don't give a vector size, it will be zero.
If it's zero rand()%droup1.size()/2 would be as rand()%0.
Returning % the reminder of a division you will get an error caused by the division by zero.

And was cnt declared?
Also, (rand() % group1.size()/2)==((rand() % group1.size())/2), not (rand() % (group1.size()/2))
Group1r does not have a size but is the vector being pushed back at the time. Group1 on the other hand has a size of 12. Cnt was declared earlier in the code as an int.
Last edited on
Just split it up to debug it. cout the results of the expression broken down into parts and identify the problem.

If I had to guess, I would say there might be a float/double in there somewhere.
Topic archived. No new replies allowed.