Monday, February 13, 2017

Programming Challenge 7.14 - Lottery Application

/* Lottery Application - This program simulates a lottery. The program
   has an array of five integers named lottery and generates a random
   in the range of 0 through 9 for each element in the array. The user
   is asked to enter five digits, which are stored in an integer array
   named user.
  
   The program compares the corresponding elements in the two arrays
   and keeps count of the digits that match. For example, the following
   shows the lottery array and the user array with sample numbers stored
   in each:
  
        Lottery array:
      * [7][4][9][1][3]

        User array:
      * [4][2][9][7][3]

   The program displays the random numbers stored in the lottery array
   and the number of matching digits. If all of the digits match, a
   message proclaiming the user as a grand prize winner is displayed. */

#include "Utility.h"

/* Function Prototypes: Display menu, Get numbers, Generate random numbers,
   Compare numbers, Display result */
void displayMenu();
void getNumbers(int[], const int);
void generateRndNumbers(int[], const int);
int compareNumbers(const int[], const int[], int[], const int);
void displayResult(const int[], const int[], int[], const int,
                   int);

int main()
{
   /* Call: displayMenu */
   displayMenu();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayMenu

   This function displays a menu with the following items:

   Enter lottery numbers (get the user numbers)
   Start drawing (generate random numbers)
   Display result (to view the result of the drawing)
   Leave the casino (quit the program)
   ********************************************************** */

void displayMenu()
{
   /* Constants: Numbers, Enter numbers, Start lottery, Display result,
                 Quit */
   const int NUMBERS = 5,
             ENTER_NUMBERS = 1,
             START_LOTTERY = 2,
             DISPLAY_RESULT = 3,
             QUIT = 4;

   /* Array variables: lottery, user, result */
   int lottery[NUMBERS] = { 0 };
   int user[NUMBERS] = { 0 };
   int result[NUMBERS] = { 0 };

   /* Variables: Menu choice, Count match */
   int menuChoice = 0,
       countMatch = 0;

   do
   {
      cout << "\t\tRICK'S CAFE AMERICAINE - LOTTERY DRAWING\n\n"
         << "1. Enter lottery numbers\n"
         << "2. Start drawing\n"
         << "3. Display result\n"
         << "4. Leave the Club ...\n\n"
         << "Your choice: ";
      cin >> menuChoice;
      cout << endl;

      while (menuChoice <= 0 || menuChoice > 4)
      {
         cout << "\nInvalid menu choice!\n\n";
         displayMenu();
      }

      /* DO NOT PLACE GENERATE NUMBERS AS SINGLE "CONDITION" IN A DO WHILE LOOP!!!!*/

      switch (menuChoice)
      {
         case 1:
         getNumbers(user, NUMBERS);
         break;

         case 2:   
         generateRndNumbers(lottery, NUMBERS);
         countMatch = compareNumbers(lottery, user, result, NUMBERS);
         break;

         case 3:
         displayResult(lottery, user, result, NUMBERS, countMatch);
         break;
      }

      if (menuChoice == QUIT)
      {
         cout << "Come back and don't forget ... We'll always have Paris!\n\n";
      }

   } while (menuChoice != QUIT);
}

/* **********************************************************
   Definition: getNumbers

   This function accepts the following array as its argument:

      * user[]

   It asks for five lotto numbers from the user and stores
   these in the array.
   ********************************************************** */

void getNumbers(int user[], int numbers)
{
   /* Get number (loop counter) */
   int getNum = 0;

   /* Ask for the numbers */
   for (getNum = 0; getNum < numbers; getNum++)
   {
      cout << "Please enter number " << (getNum + 1) << ": ";
      cin >> user[getNum];

      /* Validate input */
      while (user[getNum] < 0 || user[getNum] > 9)
      {
         cout << "Please enter number " << (getNum + 1) << ": ";
         cin >> user[getNum];
      }
   }
   cout << endl;
}

/* **********************************************************
   Definition: generateRndNumbers

   This accepts the following array as its argument:

      * lottery[]

   It generates and stores in each subscript a random number
   in the range of 1 through 9.
   ********************************************************** */

void generateRndNumbers(int lottery[], const int numbers)
{
   /* Constants: Highest number, Lowest number */
   const int HIGHEST = 9,
             LOWEST = 1;

   /* Seed the random number generator */
   srand((unsigned int)time(NULL));

   /* Variable: Count (loop counter) */
   int cnt = 0;

   while (cnt < numbers)
   {
      lottery[cnt] = (rand() % (HIGHEST - LOWEST + 1)) + LOWEST;
      cnt++;
   }
}

/* **********************************************************
   Definition: compareNumbers

   This function accepts the following arrays as argument:

      * user[]
      * lottery[]
      * result[]

   It compares the numbers entered by the user with those
   randomly generated. The matching numbers are then stored
   in result[] and further processed.
   ********************************************************** */

int compareNumbers(const int lottery[], const int user[], int result[],
                   const int size)
{
   /* Variable: Count (counter variable) */
   int count = 0;

   /* The ternary in this loop compares user[] to lottery[], and if there is a match
      between both, result[] gets the matching numbers */
   for (int compare = 0; compare < size; compare++)
   {
      user[compare] == lottery[compare] ? result[compare] = user[compare] :
                                                            user[compare];
       
      if (result[compare] == lottery[compare])
      {
         count += 1;
      }
   }

   /* Return: count */
   return count;
}

/* **********************************************************
   Definition: displayResult

   This function accepts the following arrays as argument:

      * lottery[]
      * user[]
      * result[]

   It displays the result of each drawing.
   ********************************************************** */
void displayResult(const int lottery[], const int user[], int result[],
                   const int size, int count)
{
   /* Variables: User numbers, Lottery numbers, Result numbers (loop
                 counters */
   int uNum = 0,
       rNum = 0,
       lNum = 0;

   /* Display: The numbers entered by the user */
   cout << "\nYou entered: " << setw(6) << right;
   for (uNum = 0; uNum < size; uNum++)
   {
      cout << user[uNum] << " ";
   }

   /* Display: The drawn numbers */
   cout << "\nDrawn numbers: " << setw(4) << right;
   for (lNum = 0; lNum < size; lNum++)
   {
      cout << lottery[lNum]  << " ";
   }
   cout << endl;

   /* The ternary in this loop determines whether the number stored
   is greater 0, and if so these numbers will be displayed if there
   are any. If there are no matching numbers, - will be displayed in
   their place */
   cout << "Matching numbers: ";
   for (rNum = 0; rNum < size; rNum++)
   {
      result[rNum] > 0 ? cout << (result[rNum]) << " " : cout << "- ";
      result[rNum] = 0;
   }
   cout << endl << endl;

   /* If all numbers match up the winner is congratulated */
   if (count == size)
   {
      cout << "\nWINNER WINNER WINNER! ALL NUMBERS MATCH!\n"
           << "YOU WIN CASABLANCA'S GRAND PRIZE!\n"
           << "$ 1,000,000.00"
           << "Go to 'Rick' for the check!\n\n";
   }
}

Example Output:








No comments:

Post a Comment