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.
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";
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
constint 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?