Lottery Program

I would appreciate it if someone could help me figure out how to keep the i variable on line 48 from incrementing when none of the elements match. For instance, if I were to input 0 0 0 0 0 for the 5 digits, Digits matched should say 0. And, if I were to input 3 0 0 0 0, Digits matched should say 1. Please help.

#include <iostream>
#include <iomanip>
using namespace std;

const int SIZE = 5;

void getPlayerDigits();
void compareDigits(int[], int[], int);
void displayDigits(int, int[], int[], int);

int main()
{
getPlayerDigits();
system("pause");
}

void getPlayerDigits()
{
int winningDigits[] = {3, 6, 9, 7, 1},
player[SIZE],
index = 0;

cout << "Enter 5 digits (add a space between each digit): ";
for (int index = 0; index < SIZE; index++)
cin >> player[index];

while(player[index] < 0 || player[index] > 9)
{
cout << "\nDo not enter a number less than 0 or greater than 9."
<< "\nRe-enter 5 digits: ";
for (int index = 0; index < SIZE; index++)
cin >> player[index];
}

compareDigits(winningDigits, player, SIZE);
}

void compareDigits(int wD[], int p[], int SIZE)
{
bool elementsEqual = true;
int index = 0,
i = 0;

while(elementsEqual && index < SIZE)
{
if (wD[index] != p[index])
elementsEqual = false;
++i;
index++;
}

if (elementsEqual)
cout << "\nCongradulations! You won the $300,000,000 jackpot!\n";
else
displayDigits(SIZE, wD, p, i);
}

void displayDigits(int SIZE, int wD[], int p[], int i)
{
cout << "\nWinning digits: ";
for (int index = 0; index < SIZE; index++)
cout << wD[index] << " ";

cout << "\nYour digits:" << setw(5);
for (int index = 0; index < SIZE; index++)
cout << p[index] << " ";

cout << "\n\nDigits matched: " << i
<< "\nThank you for playing. " << "\n" << endl;
}
I believe that This will fix your program :

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
#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 5;

void getPlayerDigits();
void compareDigits(int[], int[], int);
void displayDigits(int, int[], int[], int);

int main()
{
    int dummy;
getPlayerDigits();
cin >> dummy;
}

void getPlayerDigits()
{
int winningDigits[] = {3, 6, 9, 7, 1},
player[SIZE],
index = 0;

cout << "Enter 5 digits (add a space between each digit): ";
for (int index = 0; index < SIZE; index++)
cin >> player[index];

while(player[index] < 0 || player[index] > 9)
{
cout << "\nDo not enter a number less than 0 or greater than 9."
<< "\nRe-enter 5 digits: ";
for (int index = 0; index < SIZE; index++)
cin >> player[index];
}

compareDigits(winningDigits, player, SIZE);
}

void compareDigits(int wD[], int p[], int SIZE)
{
bool elementsEqual = true;
int index = 0,
i = 0;

while(elementsEqual && index < SIZE)
{
if (wD[index] != p[index])
elementsEqual = false;
if (wD[index] == p[index])              // If they do match, then the numbers matched increases
++i;
index++;
}

if (elementsEqual)
cout << "\nCongradulations! You won the $300,000,000 jackpot!\n";
else
displayDigits(SIZE, wD, p, i);
}

void displayDigits(int SIZE, int wD[], int p[], int i)
{
cout << "\nWinning digits: ";
for (int index = 0; index < SIZE; index++)
cout << wD[index] << " ";

cout << "\nYour digits:" << setw(5);
for (int index = 0; index < SIZE; index++)
cout << p[index] << " ";

cout << "\n\nDigits matched: " << i
<< "\nThank you for playing. " << "\n" << endl;
}
Topic archived. No new replies allowed.