need help with Lottery program

I'm new to C++ and need help. I need to write a program to simulate Lottery. need an array named winningDigits with 5 randomly generated numbers(0-9). an array named player where i ask user to input 5 digit numbers (0-9). need to compare the two arrays and display how many digits match. for example, if

winningDigits array= 7, 4, 9, 1, 3
player array = 4, 2, 9, 7, 3

"There are two matching digits , elements 2 and 4.

after user enters their numbers, program should display the winning digits, and the player's digits and tell how many digits matched.

(validate: 0-9 digits)

Here's my code:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int winningLottery(int[], int);
int playerPicks(int [], int);
int arraysMatch(int [], int []);


int main()
{
system("color f9");

const int userPicks = 5;
int lottoPicks = 5, number;

int winningDigits[lottoPicks];
int player[userPicks];
int count = 0;

playerPicks(player, userPicks);

cout << "\n\t==============================================";
cout << "\n\n\tWINNING LOTTERY NUMBERS:\n\n";
cout << winningLottery(winningDigits, lottoPicks);
cout << "\n\t==============================================";


cout << "\n\tYou matched " << number;
cout << " ";



system("pause");
return 0;
}

int playerPicks(int player[], int userPicks)
{
int pick;
userPicks = 5;

for (int pick = 0; pick < userPicks; pick++)
{ cout << "\n\tEnter your Lottery Pick#" << (pick + 1) << ": ";
cin >> player[userPicks];

while ((player[userPicks] < 0) || (player[userPicks] > 9))
{ cout << "\n\t\tPlease enter a number between 0 and 9: ";
cin >> player[userPicks];
}

}

}
int winningLottery(int winningDigits[], int lottoPicks)
{
int size = 5;
int pick;
winningDigits[lottoPicks];

srand((unsigned)time(0));

for(int pick=0; pick < size; pick++)
{ winningDigits[pick] = 1+(rand()%9);

cout << "\t" << winningDigits[pick] << " ";

}
return pick;
}

int arraysMatch(int winningDigits[], int player[])
{
int userPicks = 5;
int number;

for (int count = 0; count < userPicks; count++)
{
while (winningDigits[count] == player[count])
{
winningDigits[count] = number;
cout << "Your number " << count+1 << ":" << number << " matched winning"
<< " number " << count +1;
}

}
return number;
cout << endl << endl;

}
Last edited on
Whats your problem exactly?
when i run it, it doesn't compare and display numbers that are matched.

in a practice run output is:
    
Enter your Lottery Pick#1: 8

Enter your Lottery Pick#2: 3

Enter your Lottery Pick#3: 5

Enter your Lottery Pick#4: 2

Enter your Lottery Pick#5: 7

============================

Winning Lottery Numbers:

7          2           5           9           1    0
============================
You matched 1987281253    Press any key to continue.....



I don't know where the 0 came from in winning number list or the numbers 1987281253????

It's not comparing players numbers with winning numbers and displaying which elements match
Last edited on
Its kinda hard to see since you didnt use code tags and im on my iphone but in playerpicks arent you just passing by value instead of reference?
for matching elements -- see how user entered 5 (element 2) and winning number 5 (element 2)
here's my code again:

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

using namespace std;

int winningLottery(int[], int);
int playerPicks(int [], int);
int arraysMatch(int [], int []);


int main()
{
    system("color f9");
    
    const int userPicks = 5;
    int lottoPicks = 5, number;
    
    int winningDigits[lottoPicks];
    int player[userPicks];
    int arrayMatch[number];
    int count = 0;
     
    playerPicks(player, userPicks);
    
    cout << "\n\t==============================================";  
    cout << "\n\n\tWINNING LOTTERY NUMBERS:\n\n";
    cout << winningLottery(winningDigits, lottoPicks);      
    cout << "\n\t==============================================";  
             
            
    cout << "\n\tYou matched " <<  arrayMatch[number];
    cout     << " -  ";

    
    
    system("pause");
    return 0;
}

int playerPicks(int player[], int userPicks)
{
    int pick;
    userPicks = 5;
    
    for (int pick = 0; pick < userPicks; pick++)
        {   cout << "\n\tEnter your Lottery Pick#" << (pick + 1) << ": ";
            cin >> player[userPicks];
             
            while ((player[userPicks] < 0) || (player[userPicks] > 9))
                   { cout << "\n\t\tPlease enter a number between 0 and 9: ";
                     cin >> player[userPicks];
                    }
            
        }
   
}
int winningLottery(int winningDigits[], int lottoPicks)
{ 
    int size = 5;
     int pick;
          winningDigits[lottoPicks];
 
          srand((unsigned)time(0)); 
     
    for(int pick=0; pick < size; pick++) 
       { winningDigits[pick] = 1+(rand()%9); 
         
        cout << "\t" << winningDigits[pick] << " - ";
        
        }
 return pick;
}

int arraysMatch(int winningDigits[], int player[])
{
     int userPicks = 5;
     int number;
     
    for (int count = 0; count < userPicks; count++)
    {
        while (winningDigits[count] == player[count])
           {
              winningDigits[count] = number;
              cout << "Your number " << count+1 << ":" << number << " matched winning"
                   << " number " << count +1;
              }
        
    }
        return number;
        cout << endl << endl;
              
}

    
arrayMatch[number]

number is uninitialized here - do you really need 3 separate variables to keep track of the '5'?

cout << "\n\tYou matched " << arrayMatch[number];
With number = 5, this outputs the 6th element of the (0 based) arrayMatch, so it's just going to be gibberish.
rollie-

I'm trying to get arrayMatch to compare elements of the two arrays winningDigits and player, and then display what matched.

Ideas?

Honestly, your understanding of how arrays work looks like it could use some work. I would spend some time going back to learn the basics of arrays - what do the brackets mean when declaring an array vs what they mean when working with an existing array, how indices work, what the max index you can safely use for any given array is, etc.
you're right i am confused. this is my first class into programming and i am not being given anything other than read the book and google for the answer. i could use lectures/videos on subject. any ideas?
Last edited on
Just google "c++ array tutorial". Here's one you can start with:
http://www.cplusplus.com/doc/tutorial/arrays/

there are probably numerous videos if you prefer as well. Go through a few of these, get a firm understanding of arrays, then give this another shot. Understanding pointers is also very important, but if your class hasn't gotten there yet, don't worry about it yet.
Videos would help me the most since I'm more of a visual/audio learner. I really want to learn how to do this. Thank you!
Topic archived. No new replies allowed.