Lottery Simulator Homework Problem

Hey all, I've been working on some code for a homework assignment in my Computer Science I class, and for some reason the next function simply won't run. I'm completely stumped at this point, I have no idea what to change to make it run properly.

The assignment is as follows:

Write a program that simulates a lottery. The program should have an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element in the array. The user should enter five digits which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4).

lottery array:
5 2 6 9 1

user array:
1 3 6 8 1

The program should display the random numbers stored in the lottery array and the number of digits matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.


There are some additional requirements from the teacher, including having specific functions, which is why my code is perhaps more cumbersome than necessary. Here is what I have so far:

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
#include <iostream>	// for cin and cout streams
#include <cstdlib>	// for the rand and srand functions
#include <ctime>	// for the time function

//=======================================================================================

// Constant Declarations
// ---------------------

const int lotteryDigits = 9;
const int SIZE = 5;

//=======================================================================================

// Function Prototypes
// -------------------

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

//=======================================================================================

// -------------
// Main Function
// -------------

//=======================================================================================

using namespace std;

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

	//Function Calls
	generateLottery(lottery, SIZE, lotteryDigits);

	userInput(user, SIZE);

	matchCounter(lottery, user, matches);

	displayNumbers(lottery, user);

	winnerOrLoser(matches);

	system("pause");
	return 0;
} //end main

//=======================================================================================

// --------------------
// Function Definitions
// --------------------

//=======================================================================================

// Randomly generates winning lottery numbers

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

	for (int count=0; count<SIZE; count++)
	{
		lottery[count] = 1 + rand() % lotteryDigits;
	}
} // end generateLottery

//=======================================================================================

// Reads user lottery number choices

void userInput(int user[], int)
{	
	cout << "This program will simulate a lottery.\n\n";

	for (int count1=0; count1<SIZE; count1++)
	{
		cout << "Enter a digit between 0 and 9:---> ";
		cin >> user[count1];

		while (user[count1]<0 || user[count1]>9)
		{
			cout << "Error! Entry must be between 0 and 9:---> ";
			cin >> user[count1];
		}
	}
} // end userInput

//=======================================================================================

// Counts the number of matches

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

	while (check <= SIZE)
	{
		if (lotto[check] != input[check])
			check++;
		else
			match = match + 1;
	}

	return match;
} // end matchCounter

//=======================================================================================

// Diplays the winning numbers and the user's numbers

void displayNumbers(int lottery[], int user[])
{
	cout << "The 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;
} // end displayNumbers

//=======================================================================================

//Displays the number of matches and whether or not the user has won

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

	if (matches = SIZE)
		cout << "Congratulations--you WIN!!!!";
	else
		cout << "Sorry--you lose. Better luck next time.";
} // end winnerOrLoser

//=======================================================================================




When I go to run this code, it simply stops working after userInput runs. Nothing happens, not even the usual "Press any key to continue..." Just a blinking cursor. I can't figure out why the next function refuses to happen. There are no build errors, and it looks right as far as I can tell, though I am just starting out in learning C++. If any of you could help me identify and fix the problem(s), I would GREATLY appreciate it.

Thanks so much.

PS: It is formatted the way it is for class, so that can't change. Not that it should really matter, I suppose.
There is a problem with your matchCounter function. I don't know if that's what is causing the problem, but you should still fix it.

Right now, there is a possibility that it is entering an infinite loop if any one of the numbers match. What you describe sounds like an infinite loop.

Another problem with the function is that you're declaring the lotto and input arrays in the function. Aren't you supposed to be comparing the arrays that are passed to the function instead?
Ok, so I fixed the infinite loop, and got rid of the declarations inside the function. But now matches don't register... It always displays 0 matches even if some numbers match. I'm not sure what I'm doing wrong. This function seems to be the crux of my problems. Perhaps someone could be so kind as to write out how they would do it and explain why? Please remember that I am only just learning how to do this.

Here's what I have now:

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
#include <iostream>	// for cin and cout streams
#include <cstdlib>	// for the rand and srand functions
#include <ctime>	// for the time function

//=======================================================================================

// Constant Declarations
// ---------------------

const int lotteryDigits = 9;
const int SIZE = 5;

//=======================================================================================

// Function Prototypes
// -------------------

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

//=======================================================================================

// -------------
// Main Function
// -------------

//=======================================================================================

using namespace std;

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

	//Function Calls
	generateLottery(lottery, SIZE, lotteryDigits);

	userInput(user, SIZE);

	matchCounter(lottery, user, matches);

	displayNumbers(lottery, user);

	winnerOrLoser(matches);

	system("pause");
	return 0;
} //end main

//=======================================================================================

// --------------------
// Function Definitions
// --------------------

//=======================================================================================

// Randomly generates winning lottery numbers

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

	for (int count=0; count<SIZE; count++)
	{
		lottery[count] = 1 + rand() % lotteryDigits;
	}
} // end generateLottery

//=======================================================================================

// Reads user lottery number choices

void userInput(int user[], int)
{	
	cout << "This program will simulate a lottery.\n\n";

	for (int count1=0; count1<SIZE; count1++)
	{
		cout << "Enter a digit between 0 and 9:---> ";
		cin >> user[count1];

		while (user[count1]<0 || user[count1]>9)
		{
			cout << "Error! Entry must be between 0 and 9:---> ";
			cin >> user[count1];
		}
	}
} // end userInput

//=======================================================================================

// Counts the number of matches

int matchCounter(int lotto[], int input[], int match)
{
	bool numMatch = true;

	for (int check = 0; check <= SIZE; check++)
	{
		if (lotto[check] != input[check])
			numMatch = false;
		check++;
	}

	return match;
} // end matchCounter

//=======================================================================================

// Diplays the winning numbers and the user's numbers

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;
} // end displayNumbers

//=======================================================================================

//Displays the number of matches and whether or not the user has won

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

	if (matches != SIZE)
		cout << "\nSorry--you lose. Better luck next time.\n";
	else
		cout << "\nCongratulations--you WIN!!!!\n";
		
} // end winnerOrLoser

//======================================================================================= 


Thanks again.
Bumping for great homework completion
Several problems in your code :

1) Your matchCounter() function simply doesn't count the number of matches.
It just checks for a perfect match. But does nothing with the result.

2) It returns match, which is its unmodified input parameter.

3) The return value of the function is not saved. So winnerOrLoser() uses the value assigned in main(), ie 0.

4) Also, matchCounter() reads lotto[SIZE] and input[SIZE], which it shouldn't.

5) Functions userInput() and void generateLottery() are missing arguments.


At least you need to rewrite matchCounter() so that it effectively counts and returns the number of matches and make sure that winnerOrLoser() uses the returned value.
Last edited on
Alright, I made the changes you suggested (or at least I think I did), but the match counter still won't increase. Everything else seems to be okay, but for some reason the match counter simply won't increase. I don't understand what I'm doing wrong.

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
//=======================================================================================

// Constant Declarations
// ---------------------

const int lotteryDigits = 10;
const int SIZE = 5;

//=======================================================================================

// Function Prototypes
// -------------------

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

//=======================================================================================

// -------------
// Main Function
// -------------

//=======================================================================================

using namespace std;

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

	//Function Calls
	generateLottery(lottery, SIZE, lotteryDigits);

	userInput(user, SIZE);

	matchCounter(lottery, user, matches);

	displayNumbers(lottery, user);

	winnerOrLoser(matches);

	system("pause");
	return 0;
} //end main

//=======================================================================================

// --------------------
// Function Definitions
// --------------------

//=======================================================================================

// Randomly generates winning lottery numbers

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

	for (int y=0; y<SIZE; y++)
	{
		lottery[y] = rand() % lotteryDigits;
	}
	
	return lottery[0], lottery[1], lottery[2], lottery[3], lottery[4];
} // end generateLottery

//=======================================================================================

// Reads user lottery number choices

int userInput(int user[], int)
{	
	cout << "This program will simulate a lottery.\n\n";

	for (int y=0; y<SIZE; y++)
	{
		cout << "Enter a digit between 0 and 9:---> ";
		cin >> user[y];

		while (user[y]<0 || user[y]>9)
		{
		cout << "Error! Entry must be between 0 and 9:---> ";
		cin >> user[y];
		}
	}

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

//=======================================================================================

// Counts the number of matches

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;
} // end matchCounter

//=======================================================================================

// Diplays the winning numbers and the user's numbers

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;
} // end displayNumbers

//=======================================================================================

//Displays the number of matches and whether or not the user has won

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

	if (matches != SIZE)
		cout << "\nSorry--you lose. Better luck next time.\n";
	else
		cout << "\nCongratulations--you WIN!!!!\n";
		
} // end winnerOrLoser

//======================================================================================= 
It's problem 3).

Replace line 42 with
 
matches = matchCounter(lottery, user, matches);

Replace line 85 with this:

printf( "\nEnter digit # %d of 5 Between 0 and 9:---> ", y+1 ) ;

It's good that the user input is being accepted
and it's good to know what to expect.




btw, I hate using that "5" like that, hard coded, as it really "Should" be another var, but, hey, that's another matter.
Last edited on
Topic archived. No new replies allowed.