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
|
// Prog Challeg 8.2 Lottery Winners.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
//Lottery Winners
#include <iostream>
#include <cstdlib>
using namespace std;
const int LUCKY_NUMS =10;
// Function prototype that searches winning ticket number
int ticketSearch(const int [], int, int);
int main ()
{
//user continues playing lotto
char again;
const char QUIT = 'N';
//determines if player's ticket is a winner
int winningNum;
//5 digit ticket number entered by player
int playerNum;
//holds winning ticket number
int ticket;
//Array holding the winning tickets for each week
int lottoTix[LUCKY_NUMS] = {13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121};
//Player decides if they want to continue playing
for (int week = 0; week < 10; week++)
{
//Winning lotto ticket for each week (10 weeks total)
ticket = lottoTix[week];
cout << "Please enter your 5-digit ticket number for week " << (week + 1) << ": " << endl;
//Player's ticket number
cin >> playerNum;
//Calls linear search for winning lotto ticket
winningNum = ticketSearch(lottoTix, LUCKY_NUMS, playerNum);
//Error message if player's number is not the winning ticket
if ((winningNum == -1) || playerNum != ticket)
{
cout << "Sorry, you did not win the MEGAMILLIONS lottery. ";
cout << "Thanks for playing! ";
cout << "Play again? (Y/N)";
cin >> again;
}
//Player wins the lottery
else if (playerNum == ticket)
{
cout << "You have just won 598 MILLION DOLLARS!!! ";
cout << "CONGRATULATIONS!!!";
cout << "Play again? (Y/N)";
cin >> again;
}
if ((again != 'Y') || (again != 'y'))
{
//exit message
cout << "Press [Enter] to exit...\n\n";
//exits program
exit(0);
}
}
return 0;
}
//linear search for winning ticket of the week
int ticketSearch(const int ticketList[], int numTickets, int winningNum)
{
int index = 0;
int position = -1;
bool found = false;
while ((index < numTickets) && !found)
{
if (ticketList[index] == winningNum)
{
found = true;
position = index;
}
index ++;
}
return position;
}
|