/* Bank Accounts Program */ #include #include #include using namespace std; //constant definitions const int MAX_NUM = 50; // Function Prototypes int read_accts(int acctnum_array[], double balance_array[], int max_accts); void print_accts(int acctnum_array[], double balance_array[], int num_accts, ofstream &dbfile); void menu(); int findacct(int acctnum_array[], int num_accts, int requested_account); void balance(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile); void deposit(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile); void withdrawal(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile); int new_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile); int delete_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile); void pause(void); int main() { int acctnum_array[MAX_NUM]; double balance_array[MAX_NUM]; int num_accts; char choice; bool not_done = true; // open output file // ofstream outfile("c:\\bc\\cis15\\pgms\\chapter_00\\prj00_01BankAccounts\\myoutput.txt"); //comment-out for debugging ofstream outfile("con"); //un-comment for debugging outfile.setf(ios::fixed,ios::floatfield); outfile.precision(2); //set decimal precision /* first part */ /* fill and print initial database */ num_accts = read_accts(acctnum_array,balance_array,MAX_NUM); print_accts(acctnum_array,balance_array,num_accts,outfile); /* second part */ /* prompts for a transaction and then */ /* call functions to process the requested transaction */ do { menu(); cin >> choice; switch(choice) { case 'q': case 'Q': not_done = false; print_accts(acctnum_array,balance_array,num_accts,outfile); break; case 'b': case 'B': balance(acctnum_array,balance_array,num_accts,outfile); break; case 'd': case 'D': deposit(acctnum_array,balance_array,num_accts,outfile); break; case 'w': case 'W': withdrawal(acctnum_array,balance_array,num_accts,outfile); break; case 'n': case 'N': num_accts = new_acct(acctnum_array,balance_array,num_accts,outfile); break; case 'x': case 'X': num_accts = delete_acct(acctnum_array,balance_array,num_accts,outfile); break; default: outfile << "Error: '" << choice << "' is an invalid selection - try again" << endl << endl; break; } // give user a chance to look at output before printing menu pause(); } while (not_done); outfile.close(); // close output file // system("pause"); return 0; } /* Function read_accts() * Input: * acctnum_array - reference to array of account numbers * balance_array - reference to array of account balances * max_accts - maximum number of active accounts allowed * Process: * Reads the initial database of accounts and balances * Output: * Fills in the initial account and balance arrays and returns the number of active accountsPrints the database of accounts and balances */ int read_accts(int acctnum_array[], double balance_array[], int max_accts) { // open input file ifstream dbfile("c:\\bc\\cis15\\pgms\\chapter_00\\prj00_01BankAccounts\\myinput.txt"); //comment-out for debugging // ifstream dbfile("con"); //un-comment for debugging int count = 0; //initialize count while (dbfile >> acctnum_array[count] && count < max_accts) { dbfile >> balance_array[count]; count++; } dbfile.close(); return count; } /* Function print_accts: * Input: * acctnum_array - array of account numbers * balance_array - array of account balances * num_accts - number of active accounts * dbfile - reference to output file * Process: * Prints the database of accounts and balances * Output: * Prints the database of accounts and balances */ void print_accts(int acctnum_array[], double balance_array[], int num_accts, ofstream &dbfile) { dbfile << endl << endl; dbfile << "\t\tDatabase of Bank Accounts" << endl << endl; dbfile << "Account\tBalance" << endl << endl; for (int index = 0; index < num_accts; index++) { dbfile << acctnum_array[index]; dbfile << "\t$" << balance_array[index]; dbfile << endl; } return; } /* Function menu() * Input: * none * Process: * Prints the menu of transaction choices * Output: * Prints the menu of transaction choices */ void menu() { cout << endl << endl; cout << "Select one of the following transactions:" << endl; cout << "\t****************************" << endl; cout << "\t List of Choices " << endl; cout << "\t****************************" << endl; cout << "\t W -- Withdrawal" << endl; cout << "\t D -- Deposit" << endl; cout << "\t N -- New Account" << endl; cout << "\t B -- Balance Inquiry" << endl; cout << "\t X -- Delete Account" << endl; cout << "\t Q -- Quit" << endl; cout << endl << "\tEnter your selection: "; return; } /* Function findacct: * Input: * acctnum_array - array of account numbers * num_accts - number of active accounts * requested_account - requested account requested_number * Process: * Performs a linear search on the acct_nunm array for the requested account * Output: * If found, the index of the requested account is returned * Otherwise, returns -1 */ int findacct(int acctnum_array[], int num_accts, int requested_account) { for (int index = 0; index < num_accts; index++) if (acctnum_array[index] == requested_account) return index; return -1; } /* Function balance: * Input: * acctnum_array - array of account numbers * balance_array - array of account balances * num_accts - number of active accounts * outfile - reference to output file * Process: * Prompts for the requested account * Calls findacct() to see if the account exists * If the account exists, the balamce is printed * Otherwise, an error message is printed * Output: * If the account exists, the balance is printed * Otherwise, an error message is printed */ void balance(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile) { int requested_account; int index; cout << endl << "Enter the account number: "; //prompt for account number cin >> requested_account; index = findacct(acctnum_array, num_accts, requested_account); if (index == -1) //invalid account { outfile << endl << "Transaction Requested: Balance Inquiry" << endl; outfile << "Error: Account number " << requested_account << " does not exist" << endl; } else //valid zccount { outfile << endl << "Transaction Requested: Balance Inquiry" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Current Balance: $" << balance_array[index] << endl; } return; } /* Function deposit: * Input: * acctnum_array - array of account numbers * balance_array - array of account balances * num_accts - number of active accounts * outfile - reference to output file * Process: * Prompts for the requested account * Calls findacct() to see if the account exists * If the account exists, prompts for the amount to deposit * If the amount is valid, it makes the depost and prints the new balance * Otherwise, an error message is printed * Output: * For a valid deposit, the deposit transaction is printed * Otherwise, an error message is printed */ void deposit(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile) { int requested_account; int index; double amount_to_deposit; cout << endl << "Enter the account number: "; //prompt for account number cin >> requested_account; index = findacct(acctnum_array, num_accts, requested_account); if (index == -1) //invalid account { outfile << endl << "Transaction Requested: Deposit" << endl; outfile << "Error: Account number " << requested_account << " does not exist" << endl; } else //valid account { cout << "Enter amount to deposit: "; //prompt for amount to deposit cin >> amount_to_deposit; if (amount_to_deposit <= 0.00) //invalid amount to deposit { outfile << endl << "Transaction Requested: Deposit" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Error: " << amount_to_deposit << " is an invalid amount" << endl; } else //valid deposit { outfile << endl << "Transaction Requested: Deposit" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Old Balance: $" << balance_array[index] << endl; outfile << "Amount to Deposit: $" << amount_to_deposit << endl; balance_array[index] += amount_to_deposit; //make the deposit outfile << "New Balance: $" << balance_array[index] << endl; } } return; } void withdrawal(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile) { } int new_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile) { return num_accts; } int delete_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile) { return num_accts; } /* Function pause() */ void pause(void) { system("pause"); return; }