Newbie here!

Hello everyone, I am new to the coding world, taking an entry level C++ class. Trying to figure out an assignment, and at this point I am basically banging my head against the wall.

The assignment seems simple. The purpose is to simulate a "lottery" where you create two arrays, one has five random numbers between 0-9, the other has five numbers input by the user. The object is to check the number of matches the user gets correct from the list of random numbers. For some reason, I can NOT figure out how to deal with duplicates when cross referencing the arrays. I created a third array to serve as a marker for the already matched numbers, but for some reason, my code will not recognize the matches anymore. Please help! Right now I have the program spitting out the random numbers so I can see everything while testing, plus a variable to add the matches. Here is my code.

[code]
#include <iostream>
#include <ctime>
#include <cmath>



int player[5] = {}; //Arrays
int WinningDigits[5] = {};
int matched[5] = { 0, 0, 0, 0, 0 };

using namespace std;



int playercounter = 0, wincounter = 0, totalmatch = 0; //Global variables




int main()
{



srand(static_cast<unsigned int>(time(0)));
int WinningDigits[5] = { rand() % 10, rand() % 10, rand() % 10, rand() % 10, rand() % 10 }; //random numbers


cout << "Welcome to the pick five lottery! Try to guess the five winning lottery numbers.\n";
cout << "Enter five numbers between 0-9 and you could be a winner.\n";
cout << "Ready to play? Enter the first number between 0-9\n";
cin >> player[0];
cout << "Enter the second number between 0-9\n";
cin >> player[1];
cout << "Enter the third number between 0-9\n";
cin >> player[2];
cout << "Enter the fourth number between 0-9\n";
cin >> player[3];
cout << "Enter the fifth and final number between 0-9\n";
cin >> player[4];
cout << "\n";


cout << WinningDigits[0] << endl;
cout << WinningDigits[1] << endl;
cout << WinningDigits[2] << endl;
cout << WinningDigits[3] << endl;
cout << WinningDigits[4] << endl;


while (playercounter < 5 && wincounter < 5)
{
if (player[playercounter] == WinningDigits[wincounter])
{

if (matched[wincounter] != 1)
{
totalmatch++;
playercounter++;
matched[wincounter] = 1;
wincounter = 0;
}

}
if (player[playercounter] != WinningDigits[wincounter])
{
wincounter++;
if (wincounter >= 5)
{
playercounter++;
wincounter = 0;
}
}
}
cout << "Total matches = " << totalmatch << endl;





return 0;
}
My suggestion would be to change that while loop to nested for loop and then go trough the both arrays, like:
1
2
3
4
5
6
7
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {

        //and here check if player[i] == WinningDigits[j]

    }
}



also you missed [/code] at end of your post.
Last edited on
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
#include <iostream>
 #include <ctime>
 #include <cstdlib>
 
using namespace std;

int main()
{
    const int n = 5;	//number of entries
    int winningNumbers[n];
    srand(static_cast<unsigned>(time(NULL)));
    int total = 0;	//number of matches
    int x;			//user guess
	
    //fill the winningNumber array
    for(int i = 0; i < n; i++)
	winningNumbers[i] = rand()%10;		//0 - 9
	
    cout << "Welcome to the pick five lottery! Try to guess the five winning lottery numbers.\n";
     cout << "Enter five numbers between 0-9 and you could be a winner.\n";
     cout << "Ready to play? "<<endl<<endl;
	
    //enter the numbers
    for(int i = 0; i< n; i++)
    {
	cout<<"Enter number "<<i+1<<" : ";
	cin>>x;
		
	if(x == winningNumbers[i])
	    total++;
    }
	
    //print winning numbers
    cout<<endl<<"Winning numbers are"<<endl;
    for(int i = 0; i < n; i++)
	cout<<winningNumbers[i]<<" ";
	
    cout<<endl<<endl<<"You got "<<total<<" correct matches"<<endl;
	
    return 0;
}
shadowCODE, thank you!!!! That totally works. I guess the part i'm not quite understanding is the for loop with the user input. How does the loop cross reference the input with all 5 array values? It looks like it would only check 0 with 0, 1 with 1, etc. What am I missing there?
anyone got any input on that?
Hi mrob82,

this code compares the number at the time the person puts in their "guess" so they have to match then. For example,

if the winning numbers are 1,2,3,4,5
and the user puts in 1,2,3,4,5

they just got 5 out of 5 correct

if the winning numbers are 1,2,3,4,5
and the user puts in 5,4,3,2,1

they only got one right. The "3"

it compared 1 to 5 and said "nope"
it compared 2 to 4 and said "nope"
it compared 3 to 3 and said "yep"
and so on

hope this helps
Topic archived. No new replies allowed.