Wednesday, March 31, 2010

Let the user enter a charge account number and determines if the account number entered by ther user is Valid or Invalid. It uses a function to determine whether the account number is valid or invalid by checking the valid account numbers which we store in an array.

// Programming Environment: Bloodshed
// Program Description: This program lets the user enter a charge account number
//                      and determines if the account number entered by ther user
//                      is Valid or Invalid. It uses a function to determine
//                      whether the account number is valid or invalid by
//                      checking the valid account numbers which we store in an
//                      array.
// Date/Time: 3/26/2010  4:27:49 AM
//------------------------------------------------------------------------------
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
const int ACCOUNT_SIZE = 18;
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Function Prototypes
bool Search_Valid_Account(int[], int, int); // Using Bool as function return type.
//---------------------------------------------------------------------------

bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {

    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);
  
    // Storing the valid account numbers in integer array.
    int Valid_Charge_Account[ACCOUNT_SIZE] = {5658845, 8080152, 1005231, 4520125,
                                              4562555, 6545231, 7895122, 5552012,
                                              3852085, 8777541, 5050552, 7576651,
                                              8451277, 7825877, 7881200, 1302850,
                                              1250255, 4581002};  
    int Charge_Account_Num;
    int Num_Elements = 0;  
    // Getting input from user to check if it is valid or not.  
    do {
    cout << endl << "Please Enter Charge Account Number: ";
    cin >> Charge_Account_Num;    
    // Displaying the valid account numbers so that they know where they went wrong.
    cout << endl << endl << "Valid Charge Account Numbers are: " << endl << endl;
    for (int i = 0; i < 18; ++i) {
        cout << " " << Valid_Charge_Account[i];
        ++ Num_Elements;      
    }
    // If the function return true, the account is valid else its Invalid.
    if (Search_Valid_Account (Valid_Charge_Account, Charge_Account_Num,
                              Num_Elements)) {
        cout << endl << endl << "This Account Number is Valid!";
    } else {
           cout << endl << endl << "This Account Number is Invalid";
    }
  
    } while (Do_Again ());
    return 0;
}
//------------------------------------------------------------------------
// Function Definitions
bool Search_Valid_Account(int valid_charge_account[], int charge_account_num,
                          int num_elements) {
     // This function checks from index 0 to index one less than the number of
     // elements(our case: 18) if the account number entered by user is in any
     // of the index of our array of valid acount numbers. If it finds it then
     // it returns true else it return false to main.
     int index = 0;
     while (index < num_elements &&
                    charge_account_num != valid_charge_account[index]) {
           ++ index;
     }
     if (index < num_elements) {
         return true;
     } else {
         return false;
     }
}
//------------------------------------------------------------------------
bool Do_Again (void) {

    (void) fflush (stdin);
    cout << endl << endl << "\aDo Again? (y/n): ";
    return static_cast(tolower (_getch ())) == 'y';
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.