Lottery application project help

Hey guys I'm a beginner at programming and I'm trying to write an application that will generate 5 random numbers from 0-9 and then ask the user to input 5 numbers and then compare the results. I've written the code below thus far, but I need some help with it so I can get it to do a few things.

1. I need it to where the program won't generate duplicate numbers.
2. I also need it to where the user can't enter duplicate numbers as well.
3. I need the program to ask the user if the input is okay.
4. I need it to where the number doesn't have to match the same position in the array for it to count.

Thanks in advanced!

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <cstdlib>
#include <ctime>

const int lotteryNumbers = 10;
const int SIZE = 5;

int generateLottery(int[], int, int);
int userInput(int[], int);
int matchCounter(int[], int[], int);
void displayNumbers(int[], int[]);
void winOrLose(int);

using namespace std;

int main()
{
	int lottery[5] = { 0, 0, 0, 0, 0 };
	int user[5] = { 0, 0, 0, 0, 0 };
	int matches = 0;

	generateLottery(lottery, SIZE, lotteryNumbers);

	userInput(user, SIZE);

	matches = matchCounter(lottery, user, matches);

	displayNumbers(lottery, user);

	winOrLose(matches);

	system("pause");
	return 0;
}

int generateLottery(int lottery[], int, int)
{
	unsigned seed = time(0);
	srand(seed);

	for (int y = 0; y < SIZE; y++)
	{
		lottery[y] = rand() % lotteryNumbers;
	}

	return lottery[0], lottery[1], lottery[2], lottery[3], lottery[4];
}

int userInput(int user[], int)
{
	cout << "Lottery Simulator\n\n";

	for (int y = 0; y < SIZE; y++)
	{
		printf("\nEnter number # %d of 5 that's between 0 and 9: ", y + 1);
		cin >> user[y];

		while (user[y]<0 || user[y]>9)
		{
			cout << "Error, you must enter a number between 0 and 9: ";
			cin >> user[y];
		}

	}


	return user[0], user[1], user[2], user[3], user[4];
}

int matchCounter(int lotto[], int input[], int)
{
	int match = 0;

	for (int x = 0; x < SIZE; ++x)
	{
		if (lotto[x] == input[x])
			match = match + 1;
	}

	return match;
}

void displayNumbers(int lottery[], int user[])
{
	cout << "\nThe winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
	cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
}

void winOrLose(int matches)
{
	cout << "You matched " << matches << " numbers";

	if (matches != SIZE)
		cout << "\nSorry, you didn't win the jackpot. Try again.\n";
	else
		cout << "\nCongrats, you won the lottery!\n";

}
closed account (LA48b7Xj)
Here is a way to generate the numbers without duplicates.
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
#include <algorithm>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>

using namespace std;

vector<int> generate_lottery_numbers()
{
    vector<int> lottery_numbers;

    for(int i=0; i<10; ++i)
        lottery_numbers.push_back(i);

    shuffle(lottery_numbers.begin(), lottery_numbers.end(), default_random_engine(time(nullptr)));
    lottery_numbers.resize(5);
    return lottery_numbers;
}

int main()
{
    vector<int> v = generate_lottery_numbers();

    for(auto e : v)
        cout << e << '\n';
}

Last edited on
Topic archived. No new replies allowed.