Monday, March 6, 2017

Programming Challenge 8.1 - Charge Account Validation

/* Charge Account Validation - This program lets the user enter a charge account     number. The program determines if the number is valid by checking for it in the following list:
  
      5658845  4520125    7895122     8777541  8451277  1302850
      8080152  4562555    5552012     5050552  7825877  1250255
      1005231  6545231    3852085     7576651  7881200  4581002

   The list of numbers above is initialized in a single-dimensional array.
   A simple linear search is used to locate the number entered by the user.
   If the user enters a number that is in the array, the program displays
   a message saying that the number is valid. If the user enters a number
   that is not in the array, the program displays a message indicating that
   the number is invalid. */

#include "Utility.h"

/* Function prototypes */
int getNumber();
void searchNumber(const int[], const int, int);

int main()
{
   /* Number of elements in the array */
   const int NUMELS = 18;

   /* Array initialized with a list of account numbers */
   int accNumbers[NUMELS] = { 5658845, 4520125, 7895122, 8777541,
                                                    8451277, 1302850, 8080152, 4562555,
                                                    5552012, 5050552, 7825877, 1250255,
                                                    1005231, 6545231, 3852085, 7576651,
                                                    7881200, 4581002 };

   int secretNum = 0;

   secretNum = getNumber();
   searchNumber(accNumbers, secretNum, NUMELS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getNumber

   This function asks the user for his or her secret account
   verfication number and returns it.
   ********************************************************** */

int getNumber()
{
   int secretNum = 0;

   cout << "\n\t\tAshikaga Bank - Credit Authorization Terminal\n\n"
        << "Please enter your secret number: ";
   cin >> secretNum;

   return secretNum;
}

/* **********************************************************
   Definition: searchNumber

   This function accepts accNumbers, containing a list of int
   numbers secretNumber, and NUMELS as arguments. It performs
   a linear search on the array. If a match has been found,
   the user is informed that his or her entry was valid. If
   no match could be found, a message indicating that the
   secret number was not found is displayed.
   ********************************************************** */

void searchNumber(const int accNumbers[], const int secretNum,
                  int NUMELS)
{
   int index = 0;
  
   /* Flag to indicate a match */
   bool isFound = false;
  
   while (index < NUMELS && !isFound)
   {
      /* If secretNumber is found, isFound is set to true and the loop
         exits. Otherwise the array is searched to the end, isFound gets
         false, and the loop exits. */
      accNumbers[index] == secretNum ? isFound = true : isFound = false;
      index++; 
   }

   isFound ? cout << "\nYour secret number has been successfully verified!" :
             cout << "\nYour secret number was not found in our database ...";
}

Example Output: 




No comments:

Post a Comment