// Torrez, Elaine CS1A P. 447, #13
// LOTTERY APPLICATION
// *****************************************************************************************
// * This program simulates a simple lottery game. It generates five random numbers *
// * (0–9) and asks the user to enter five guesses. The program compares the user’s *
// * numbers to the lottery numbers and counts how many match. If all match, the user *
// * wins the grand prize. *
// * *
// * INPUT: *
// * user[] : The five numbers entered by the user (0–9) *
// * *
// * OUTPUT: *
// * lottery[]: The five randomly generated lottery numbers (0–9) *
// * matches : The number of positions where user and lottery numbers match *
// * message : Whether the user wins or not *
// *****************************************************************************************
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time() to seed random numbers
using namespace std;
int main()
{
const int SIZE = 5;
int lottery[SIZE]; // stores lottery numbers
int user[SIZE]; // stores user guesses
int matches = 0; // counter for matching digits
// Seed random number generator
srand(time(0));
// Generate random lottery numbers (0–9)
for (int i = 0; i < SIZE; i++)
{
lottery[i] = rand() % 10;
}
// Get user's lottery numbers
cout << "Enter your 5 lottery numbers (each between 0 and 9):\n";
for (int i = 0; i < SIZE; i++)
{
cout << "Number " << (i + 1) << ": ";
cin >> user[i];
// Input validation
while (user[i] < 0 || user[i] > 9)
{
cout << "Please enter a number between 0 and 9: ";
cin >> user[i];
}
}
// Compare arrays
for (int i = 0; i < SIZE; i++)
{
if (user[i] == lottery[i])
matches++;
}
// Display both arrays
cout << "\nLottery Numbers: ";
for (int i = 0; i < SIZE; i++)
cout << lottery[i] << " ";
cout << "\nYour Numbers: ";
for (int i = 0; i < SIZE; i++)
cout << user[i] << " ";
// Display results
cout << "\n\nYou matched " << matches << " number(s)." << endl;
if (matches == SIZE)
cout << " GRAND PRIZE WINNER! All numbers match! \n";
else
cout << "Better luck next time!\n";
return 0;
}