Counting is always incorrect

I've been searching for hours on how to fix this but I'm stumped. It's the simply lottery program, where 5 digits are randomly generated and compared to the user's 5. It's supposed to count how many are the same, but every time it gives either 1 or 0, which is usually incorrect. I would greatly appreciate any help on fixing this issue.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int number = 5;
int winningDigits[number];
const int value = 5;
int player[value];
int numMatches = 0;


void generateNumbers()
{
	//random number generator
	srand(time(NULL));

	//Generates the 5 winning numbers
	for (int i = 0; i < number; i++)
	{
		winningDigits[0] = rand() % 10;
		winningDigits[1] = rand() % 10;
		winningDigits[2] = rand() % 10;
		winningDigits[3] = rand() % 10;
		winningDigits[4] = rand() % 10;

	}

}

//user's numbers
void playerInput()
{

	cout << "Enter your five numbers:\n";
	cin >> player[0];
	cin >> player[1];
	cin >> player[2];
	cin >> player[3];
	cin >> player[4];

}

//outputs the two sets of numbers
void displayValues()
{

	cout << "Winning Digits:\n";
	for (int i = 0; i < number; i++)
	{
		cout << " " << winningDigits[i];
		cout << endl;
	}

	cout << "Your numbers:\n";
	for (int i = 0; i < value; i++)
	{
		cout << " " << player[i];
		cout << endl;
	}
}

int main()
{
	int i = 0;

//compare and count
	generateNumbers();
	playerInput();

	for (int i = 0; i < 5; i++)
	{
		if (winningDigits[i] == player[i])
			numMatches++;
	}

	displayValues();

//Says if they won or not, if they didn't how many were the same
	if (numMatches != 5)
	{

		cout << numMatches << " of your numbers matched.\n";
	}

	if (numMatches == 5)
	{
		cout << "Congratulations! You won the lottery!\n";
	}


	system("pause");
	return 0;
}
Last edited on
Does the order of the numbers matter?
Not particularly, but I'm just counting how many repeat and outputting that, not necessarily showing the user the exact digits. I'm not sure exactly what you were referring to but I hope I answered it.
Last edited on
Imagine the user enters 1 3 5 7 9
Imagine lottery numbers 5 3 2 4 1

You only compare 1 and 5, 3 and 3, 5 and 2 and so on.
Do you see the problem now?

Easiest solution would be to sort the two arrays first.
Another option would be to create a table and count the occurences of each number.
Each number that appears twice or more is a winner.
There are 2 things you could possibly use for your program.
You could use a linear search or a binary search.
I wrote a similar program for my c++ class a week or 2 ago.
For the linear search you can use (or your own version):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int linearSearch(int list[], int max, int win)
{
	int i = 0;
	int position = -1;
	bool found = false;
	
	while(i < max && !found)
	{
		if(list[i] == win) //value found
		{
			found = true;   
			position = i;
		}
		i++;
	}
	return position;
}

Or you could use the binary search:
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

int binarySearch(int list[], int max, int win)
{
	int first = 0;
	int last = max - 1;
	int middle;
	int position = -1;
	bool found = false;
	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if(list[middle] == win) 		// middle
		{
			found = true;
			position = middle;
		}
		else if (list[middle] > win)     // Lower half
		{
			last = middle - 1;
		}
		else
		{
			first = middle + 1;
		}
	}
	return position;
}



Topic archived. No new replies allowed.