I'm stuck on this assignment, which is:
Write a program that simulates a lottery. The program should have an array of 5 integers named winningDigits, with a randomly generated number in the range of 0 through 9 for each element in the array. The program should ask the user to enter 5 digits and should store them in a second integer array named player. The program must compare the corresponding elements in the two arrays and count how many digits match. For example, the following shows the winningDigits array and the Player array with sample numbers stored in each. There are two matching digits, elements 2 and 4. Once the user has entered a set of numbers, the program should display the winning digits and the player’s digits and tell how many digits matched.
We're not supposed to use functions.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
usingnamespace std;
int main()
{
int winningDigits[5];
int playerDigits[5];
srand(time(0));//all you need
cout << "Enter five numbers: " << endl;
int num;
for (int i = 0; i < 5; i++)
{
cin >> num;
while (num < 0 || num > 9)//should use a while to force user to enter correct number
{
cout << "Number must be greater than 0 and less than 9" << endl;
cout << "Please re-enter number: " << endl;
cin >> num;
}
playerDigits[i] = num;//need to add the variable to the array
}
cout << "Your numbers are: " << endl;
for (int i = 0; i < 5; i++)
{
cout << playerDigits[i] << " ";
}
cout << endl;
cout << "The five winning numbers are: " << endl;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 9 + 1;
cout << winningDigits[i] << " ";
}
cout << endl;
cout << "Matching digits are: ";
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int n = 0; n < 5; n++)//need to use a nested for loop
{
if (winningDigits[i] == playerDigits[n])
{
count += 1;
cout << playerDigits[n] << " ";
}
}
}
cout << endl;
cout << "There are " << count << " matches." << endl;
return 0;
}
for (int i = 0; i < 5; i++)
{
for (int n = 0; n < 5; n++)//need to use a nested for loop
{
if (winningDigits[0] == playerDigits[0])
{
count += 1;
}
if (winningDigits[1] == playerDigits[1])
{
count += 1;
}
if (winningDigits[2] == playerDigits[2])
{
count += 1;
}
if (winningDigits[3] == playerDigits[3])
{
count += 1;
}
if (winningDigits[4] == playerDigits[4])
{
count += 1;
}
}
}
cout << endl;
cout << "There are " << count << " matches." << endl;
but this did not output the correct number of matches. We are supposed to compare each element of the arrays, and even though the elements did not match but another element of the first array matched with a different element from the second array, the program prints that there is a match. I don't know what I'm doing wrong, can you please help? Thank you so much in advance!