lottery program with function

I have written a program for the prompt,
"You’re going to write a program that models the Littleton City Lotto (not a real Lotto game). The program is going to allow to user to first select their lotto numbers. The program will then randomly generate the winning lotto numbers for the week and then check the winning numbers against the random ticket the user played in the Lotto to see how many numbers the user guessed correctly."

I have a lot of code written for this, I am having trouble with user ticket numbers matching the winning numbers. When I run the program and get matching numbers to the winning numbers the counter I have set up doesn't go up, so there is never any winning ticket. I know it is a lot of code, so anyone that helps out it is greatly appreciated. The problem is in the functions results and checkmatches

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

void getuserTicket(int userTicket[], int);
void getwinningNumbers(int winningNums[], int);
void results(string name, int userTicket[], int winningNums[], int size);
void playerMenu();
int checkMatches(int userTicket[], int winningNums[], int size);
bool checkDuplicates(int userticket[],int size , int i );


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

	const int num = 7;
	int userTicket [num]; // array holds user picked numbers
	int winningNums [num]; // array holds random winning numbers
	int matchingNumbers = 0;
	string name1; 
	char choice;

	playerMenu();
	cin >> choice;
	cout << endl;
	if (choice == '1')
	{
		cout << "Please enter your name: ";
		cin >> name1;
		cout << endl;

		getuserTicket(userTicket, num);
		getwinningNumbers(winningNums, num);
		cout << endl;
		results(name1, userTicket, winningNums, num);
		
	}
	else if (choice == 'q' || choice == 'Q')
	{
		cout << "You have chosen to quit the program" << endl;
	}
	else if (choice != '1' || choice != 'q' || choice != 'Q')
	{
		cout << "Invalid selection" << endl;
	}

	return 0;
}

/***************************************
FUNCTION NAME: playerMenu

INPUT:

OUTPUT:

DESCRIPTION:
***************************************/
void playerMenu()
{
	cout << "\nLITTLETON CITY LOTTO MODEL:" << endl
		<< "---------------------------" << endl
		<< "1) Play Lotto" << endl
		<< "q) Quit Program" << endl
		<< "Please make a selection" << endl;
}

/***************************************
FUNCTION NAME: playAgain

INPUT:

OUTPUT:

DESCRIPTION:
***************************************/
bool playAgain()
{
	char choice;
	cout << " " << endl;
	cout << "If you would like to play again press Y/y\n"
		<< "otherwise press any key to exit the game" << endl;
	cin >> choice;
	if (choice == 'y' || choice == 'Y')
	{
		choice = true;
	}
	else
		choice = false;

	return choice;
}

/***************************************
FUNCTION NAME: getuserTicket

INPUT:

OUTPUT:

DESCRIPTION: Get user lotto numbers 
	between 1 and 40 with no duplicate
	entries.
***************************************/
void getuserTicket(int userTicket[], int size)
{
	// Get user lotto numbers
	cout << setw(55) << "Enter 7 lottery numbers between the numbers 1-40: " << endl;

	//Loop to store 7 lotto numbers in array 
	for (int i = 0; i < size; i++)
	{
		cout << "selection # " << i + 1 << ": ";
		cin >> userTicket[i];

		//Loop if user enters invalid numbers
		while (checkDuplicates (userTicket, userTicket[i], i) || userTicket[i] < 1 || userTicket[i] > 40)
		{
			if (userTicket[i] < 1 || userTicket[i] > 40)
			{
			cout << "Invalid number" << endl;
			cout << "Enter a number between 1 and 40: ";
			cin >> userTicket[i];
			//checks for duplicate numbers in array
			}
			if (checkDuplicates(userTicket, userTicket[i], i))
			{
				cout << "You have already entered that number, please choose a different number: " << endl;
				cin >> userTicket[i];
				cout << endl;
			}
		}
	}
}

/***************************************
FUNCTION NAME: getuserTicket

INPUT:

OUTPUT:

DESCRIPTION:
***************************************/
void getwinningNumbers(int winningNums[], int size)
{
	srand(time(0));

	for (int i = 0; i < size; i++)
	{
		winningNums[i] = 1 + (rand() % 40);
		
		while (checkDuplicates(winningNums, winningNums[i], i))
		{
			winningNums[i] = 1 + (rand() % 40 );
		}
	}
}


/***************************************
FUNCTION NAME: Results

INPUT:

OUTPUT:

DESCRIPTION: displays lotto resluts
***************************************/
void results(string name,int userTicket[], int winningNums[], int size)
{
	int count = checkMatches(userTicket, winningNums, size);
	cout << "" << name << " LOTTO RESULTS:" << endl;
	cout << "---------------------------" << endl;

	cout << "WINNING TICKET NUMBERS: ";
	for (int i = 0; i < size; i++)
	{
		cout << " " << winningNums[i];
	}
	cout << endl;
	cout << "" << name << "'s TICKET: ";
	for (int i = 0; i < size; i++)
	{
		cout << " " << userTicket[i];
	}
	cout << endl << endl;
	cout << "RESULTS:" << endl;
	cout << "--------" << endl;
	cout << "Number Matches: " << count << endl;
	cout << endl;
	cout << "Winnings : " ;
	switch (count)
	{
		case 1:
		case 2:
			cout << "SORRY NOTHING\n\n";
			break;
		case 3:
			cout << "FREE TICKET\n\n";
			break;
		case 4:
			cout << "NOT BAD - $100\n\n";
			break;
		case 5:
			cout << "LUCKY YOU! - $5,000\n\n";
			break;
		case 6:
			cout << "GREAT! - $100,000\n\n";
			break;
		case 7:
			cout << "JACKPOT - 1 MILLION\n\n";
			break;
	}
	cout << endl;
}

/***************************************
FUNCTION NAME: checkMatches

INPUT:

OUTPUT:

DESCRIPTION: Compares user lotto numbers
	to random generated numbers to see
	how many matches there are.
***************************************/
int checkMatches(int userTicket[], int winningNums[], int size)
{
	int count = 0;

	for (int i = 0; i < size; i++)
	{
		int temp = userTicket[i];

		if (temp == winningNums[i]) 
		{
			count++;
		}
	}
	return count;
}

/***************************************
FUNCTION NAME: checkDuplicates

INPUT:

OUTPUT:

DESCRIPTION: checks for duplicate 
	numbers. 
***************************************/
bool checkDuplicates(int userticket[], int ticket, int i)
{
	bool duplicate = false;

	for (int j = 0; j < i; j++)
	{
		if (userticket[j] == ticket)
		{
			duplicate = true;
		}
	}
	return duplicate;
}
Last edited on
Your checkMatches function only compares userTicket[i] with winningNums[i], you compare userTicket[0] with winningNums[0], but what if the first user number matches the SECOND winning number?

For example, you code considers this to be ZERO matches:

User numbers - 2 3 4 5 6 7

Winning numbers - 1 2 3 4 5 6

I see that now, removing the brackets doesnt work either and i am not sure how to have it check all of the numbers in the array.

[update] changing "i" to a different variable fixed it! thank you for pointing out with issue :D
Last edited on
Topic archived. No new replies allowed.