// Marvyn Jesse De La Torre CS1A Chapter 7, pg. 447, #13
//
/****************************************************************************
* RUN THE LOTTERY APPLICATION
*
* This program accepts five random lottery digits from 0 through 9.
* It asks the user to enter five digits and compares the user's numbers
* with the lottery numbers. It displays the lottery numbers and the number
* of matching digits. Matching all five digits makes the user a grand
* prize winner.
*
* INPUT
* user // The five lottery digits entered by the user
*
* OUTPUT
* lottery // The five randomly generated lottery digits
* matches // Number of matching digits
* prizeMessage // Whether the user won the grand prize
****************************************************************************/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
const int SIZE = 5;
int lottery[SIZE];
int user[SIZE];
int matches = 0;
// Seed the random number generator.
srand(static_cast<unsigned int>(time(0)));
// Generate five random lottery digits from 0 through 9.
for (int i = 0; i < SIZE; i++)
{
lottery[i] = rand() % 10;
}
// Ask the user to enter five digits and validate each one.
cout << "Enter five lottery digits (0 through 9).\n";
for (int i = 0; i < SIZE; i++)
{
cout << "Enter digit " << i + 1 << ": ";
cin >> user[i];
while (cin.fail() || user[i] < 0 || user[i] > 9)
{
cout << "Invalid digit. Enter a number from 0 through 9: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> user[i];
}
}
// Compare each user digit with the lottery digit in the same position.
for (int i = 0; i < SIZE; i++)
{
if (user[i] == lottery[i])
{
matches++;
}
}
// Display the lottery numbers and matching digit count.
cout << "\nLottery numbers: ";
for (int i = 0; i < SIZE; i++)
{
cout << lottery[i] << " ";
}
cout << "\nMatching digits: " << matches << endl;
// Check whether all five digits match.
if (matches == SIZE)
{
cout << "Congratulations! You are the grand prize winner!\n";
}
return 0;
}