Lottery Homework

Hey. So I'm currently writing this lottery program for homework, and I'm almost completed, but for whatever reason whenever I run the program it just outputs garbage data where it should output the amount of numbers guessed correctly. Here's my code.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

void generateNumbers(int [], int);
void getUser(int [], int);
int findMatches(int [], int [], int);
void displayValues(int [], int [], int);

const int size = 5;
int count = 0;
int Lottery[size];
int user[size];

int main ()
{	
	generateNumbers(Lottery, size);
	getUser(user, size);
	count = findMatches(Lottery, user, size);
	displayValues(Lottery, user, size);
	
	return 0;
}


void generateNumbers(int Lottery[], int size)
{
	srand(time(NULL));
	for(int k = 0; k < size; k++)
	{ 
		Lottery[k] = rand() % size + 1;
	}
	
}

void getUser(int user[], int size)
{
	int userInput = 0;
	
	for(int i=0; i < size; i++)
	{
		cout << "Number " << i + 1 << ": ";
		cin >> userInput;
		
		while(userInput > 9 || userInput < 0)
		{
			cout << "The number you entered is invalid.\n";
			cin >> userInput;
		}
		
		user[i] = userInput;
		
	}
}

int findMatches(int Lottery[], int user[], int size)
{
	int count;
	
	for(int n = 0; n < size; n++)
	{
		if(Lottery[n] == user[n])
		{
			count++;
		}
	}
	
	return count;
}

void displayValues(int Lottery[], int user[], int size)
{
	cout << "Lottery numbers:\n";
	for(int m = 0; m < size; m++)
	{
		cout << setw(size) << Lottery[m];
	}
	
	cout << endl;
	
	for(int p = 0; p < size; p++)
	{	
		cout << setw(size) << user[p];
	}
	
	cout << endl << endl;
	
	cout << "You matched " << count << " numbers.\n";
}


I found a problem very similar to this, but the thread got archived before I could mention my personal problem. So if anyone has any help, it would be much appreciated.
Last edited on

Line 71 you are increasing count which has not been initialised. There may be other issues but that's what stood out to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int findMatches(int Lottery[], int user[], int size)
{
	int count = 0;   // <-------- initialise it
	
	for(int n = 0; n < size; n++)
	{
		if(Lottery[n] == user[n])
		{
			count++;
		}
	}
	
	return count;
}
It worked. Thanks, guys.

You are welcome :)
Topic archived. No new replies allowed.