C++ array

I need help writing a lottery program that generates 5 non duplicate numbers that is between 1 and 20 by using arrays. Here is my code and i cant compile it.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>

using namespace std;

int main()
{
	srand(time(NULL));

	int lottery[5] = { 0, 0, 0, 0, 0 }, count = 0, rand_lottery;
	bool isdup = false;

	cout << "Your Lottery:" << endl;

	while (count < 5)
	{
		int rand_lottery, i;
		rand_lottery = rand()%20 +1;
		i =  lottery[5]
		for (int i = 0; i < 5; i++);
		{
				if (lottery[i] == rand_lottery)
				{
					isdup = true;
				}
		}
		if (isdup == false)
		{ 
			lottery[count] = rand_lottery;
			count++;
		}
	}
	for (int i = 0; i < 5; i++)
	{
		cout << lottery[i] << " ";

		return (0);

	}
}
What is in line 21? Remove it.
In lines 22-28 you check if random number already exist. So you should compare it with existed values. Rewrite "5" in line 22 as "count".
Will it work?
Last edited on
It caused more error when i remove line 21 and when i replaced count with 5 it would cause even more error :(
Disclaimer: I am a beginner.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <stdlib.h>
#include <time.h>

int main()
{
	// seed the random number generator
	srand(time(NULL));

	// initialize the array of lottery numbers
	int lottery[5];

	std::cout << "Your Lottery:" << std::endl;
	
	// loop until array has no duplicates
	bool unique = false;
	while(!unique) {
		int no_of_duplicates = 0;
		
		// create five random numbers from 1-20 and store them
		for(int i=0; i<5; i++)
			lottery[i] = rand()%20+1;

		// walk through lottery looking for duplicates
		for(int i=0; i<5; i++) {
			for(int ii=0; ii<5; ii++) {
		
				// if there is a duplicate, increase the counter
				if((lottery[ii]==lottery[i]) && (i != ii))
					no_of_duplicates++;
			}
		}
		
		// if there are no duplicates, set unique to true
		// to get out of the while loop
		if(!no_of_duplicates)
			unique = true;
	}

	// display the array
	for(int i = 0; i < 5; i++)
		std::cout << lottery[i] << " ";
		
	return 0;
}
Last edited on
Topic archived. No new replies allowed.