1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//=======================================================================================
// Constant Declarations
// ---------------------
const int lotteryDigits = 10;
const int SIZE = 5;
//=======================================================================================
// Function Prototypes
// -------------------
int generateLottery(int[], int, int);
int userInput(int[], int);
int matchCounter(int[], int[], int);
void displayNumbers(int[], int[]);
void winnerOrLoser(int);
//=======================================================================================
// -------------
// Main Function
// -------------
//=======================================================================================
using namespace std;
int main()
{
// Variable Declarations
int lottery[5] = {0, 0, 0, 0, 0};
int user[5] = {0, 0, 0, 0, 0};
int matches = 0;
//Function Calls
generateLottery(lottery, SIZE, lotteryDigits);
userInput(user, SIZE);
matchCounter(lottery, user, matches);
displayNumbers(lottery, user);
winnerOrLoser(matches);
system("pause");
return 0;
} //end main
//=======================================================================================
// --------------------
// Function Definitions
// --------------------
//=======================================================================================
// Randomly generates winning lottery numbers
int generateLottery(int lottery[], int, int)
{
unsigned seed = time(0);
srand(seed);
for (int y=0; y<SIZE; y++)
{
lottery[y] = rand() % lotteryDigits;
}
return lottery[0], lottery[1], lottery[2], lottery[3], lottery[4];
} // end generateLottery
//=======================================================================================
// Reads user lottery number choices
int userInput(int user[], int)
{
cout << "This program will simulate a lottery.\n\n";
for (int y=0; y<SIZE; y++)
{
cout << "Enter a digit between 0 and 9:---> ";
cin >> user[y];
while (user[y]<0 || user[y]>9)
{
cout << "Error! Entry must be between 0 and 9:---> ";
cin >> user[y];
}
}
return user[0], user[1], user[2], user[3], user[4];
} // end userInput
//=======================================================================================
// Counts the number of matches
int matchCounter(int lotto[], int input[], int)
{
int match = 0;
for (int x = 0; x < SIZE; ++x)
{
if (lotto[x] == input[x])
match = match + 1;
}
return match;
} // end matchCounter
//=======================================================================================
// Diplays the winning numbers and the user's numbers
void displayNumbers(int lottery[], int user[])
{
cout << "\nThe winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
} // end displayNumbers
//=======================================================================================
//Displays the number of matches and whether or not the user has won
void winnerOrLoser(int matches)
{
cout << "You matched " << matches << " numbers";
if (matches != SIZE)
cout << "\nSorry--you lose. Better luck next time.\n";
else
cout << "\nCongratulations--you WIN!!!!\n";
} // end winnerOrLoser
//=======================================================================================
|