Write a program that simulates a lottery. The program should have an array of five integers
named lottery, and should generate a random number in the range of 0 through 9 for each
element in the array. The user should enter five digits which should be stored in an integer
array named user. The program is to compare the corresponding elements in the two arrays
and keep a count of the digits that match. For example, the following shows the lottery array
and the user array with sample numbers stored in each. There two matching digits (elements
2 and 4).
Lottery array:
7 4 9 1 3
User array:
4 2 9 7 3
The program should display the random numbers stored in the lottery array and the number of
matching digits. If all of the digits match, display a message proclaiming the user as a grand
prize winner of $25,000. If there are four matching digits, the prize should be $1,000, and if
there are three matching digits, the prize should be $500.
I made it and I have problems to make it match 4 digits and 3 also.
My work is:
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
const int SIZE = 5; //Size of the arrays.
int user[SIZE]; //To hold the user's lucky numbers.
int lottery[SIZE]; //To hold the random numbers.
int count = 0; //Loop counter.
bool numMatch = true; //Flag variable.
cout << "Do you feel lucky today? If so, enter your " <<SIZE<<" lucky numbers \n";
cout << "between 0 and 9: \n";
//Get five numbers from the user and store them in the user array.
for (count = 0; count < SIZE; count++)
{
cout << "Please enter lucky number # " <<(count+1)<<":";
cin >> user[count];
}
//Generate five random numbers and store them in the lottery array.
srand((unsigned int)time(0));
for (count = 0; count < SIZE; count++)
{
lottery[count] = 1 + rand()%9;
}
//Display the contents of lottery array.
cout << "The winning numbers are: \n";
for (count =0; count < SIZE; count++)
{
cout <<" " <<lottery[count];
cout << endl;
}
//Determine whether there are matching numbers.
for (count =0; count < SIZE; count++)
{
while (numMatch && count < SIZE)
{
if (user[count] != lottery [count])
numMatch = false;
count++;
}
}
if (numMatch)
cout << "Congratulations you are the Grand Prize Winner!!!\n";
else if (numMatch = 4)
cout << "Congratulations you win 1,000!!!\n";
else if (numMatch = 3)
cout << "Congratulations you win 500!!!\n";
else
cout << "Sorry you did not pick the winning numbers, please try again.\n";
system("pause");
return 0;
}