Error in my "for" loop

Hello -
I am new to C++ and working on a random generator to create 5 numbers and place them in my populateLottery Array. For some reason, the only subscript/element getting populated is element[0}. My count that I'm using is jumping from 0 to 5 in my "for" loop. I'm stumped as I've used the "for" loop before. I thought maybe a second set of eyes would help me. My MAX_LOTTO is a constant set to "5" and "count" is my counter, initialized to 0.

//Seed the generator to create random numbers each time program executes.
srand ( time( 0 ) );

int populateLottery[MAX_LOTTO] = {0}; //Populate the Array

for (int count = 0; count < MAX_LOTTO; count++); //This is the problem area
{
populateLottery[count] = rand() % 10; //random numbers from 0-9
}
You had two errors.

Here is the fixed code.
1
2
3
4
5
6
7
8
9
int MAX_LOTTO = 5;
srand ( time( 0 ) );

int populateLottery[MAX_LOTTO]; //Populate the Array

for (int count = 0; count < MAX_LOTTO; count++) //This is the problem area
{   
   populateLottery[count] = rand() % 10;   //random numbers from 0-9
}   


I will edit it to be nice after you add code tags.
Last edited on
Hi - Thanks for the reply - this is what I have and I still am not getting my elements in my lottery array populated, with the exception of element[0]. Thank you for your help!

#include <iostream>
#include <iomanip>
#include <cstdlib> // For the random number generator pg. 129 in text book
#include <ctime> // This is needed for the time function (to generate nums)
using namespace std;

int main()
{
//Declare the variables and the arrays
const int MAX_LOTTO = 5;
const int MAX_USER = 5;
int lottery[MAX_LOTTO]; //Array for lottery nums
int user[MAX_USER]; //Array for user nums

//Seed the generator to create random numbers each time program executes.
srand ( time( 0 ) );

int lottery[MAX_LOTTO]; //Populate the Array

for (int count = 0; count < MAX_LOTTO; count++);
{
lottery[count] = rand() % 10; //random numbers from 0-9
}
return 0;
}
Last edited on
No you did NOT fix your problem, and you still not did place your code in [code] tags.

http://www.cplusplus.com/forum/articles/42672/
OK. Sorry...I understand now. I removed the ";" from my "for loop" and at least I'm now getting my array populated. It is not getting populated with 0-9 values but instead double digits. But at least I have something now. thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include <cstdlib> // For the random number generator pg. 129 in text book
#include <ctime> // This is needed for the time function (to generate nums)
using namespace std;

int main()
{
//Declare the variables and the arrays
const int MAX_LOTTO = 5;
const int MAX_USER = 5;
int lottery[MAX_LOTTO]; //Array for lottery nums
int user[MAX_USER]; //Array for user nums

//Seed the generator to create random numbers each time program executes.
srand ( time( 0 ) );

int lottery[MAX_LOTTO]; //Populate the Array

for (int count = 0; count < MAX_LOTTO; count++)
{
lottery[count] = rand() % 10; //random numbers from 0-9
}
return 0;
} 
Topic archived. No new replies allowed.