Site hosted by Angelfire.com: Build your free website today!
« January 2006 »
S M T W T F S
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
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Open Community
Post to this Blog
You are not logged in. Log in
C++ Programs from Random Places
Monday, 23 January 2006
Fun Bank Account Program
//THIS IS GOOD FOR USING STRUCTS AND ARRAYS
//**NOTE**: THIS IS NOT COPYRIGHTED MATERIAL AND IS FREE FOR
//DISTRIBUTION AND PERSONAL USE
#include
#include
#include
#include
using namespace std;

// Create bank account data type
struct acct { // bank account data
int num; // account number
string name; // owner of account
float balance;// balance in account
};

// Function Prototypes
int find(acct bank[ ], int numaccts, int goal);
void insert(acct bank[ ], int &numaccts, acct newacct);
void remove(acct bank[], int &numaccts, acct deleteacct);
void transfer(acct bank[], int &numaccts, float amount, acct account);

int main ()
//PURPOSE: simulate a small bank
//PRECONDITIONS: existing accounts in file account.txt
// in project folder
//POSTCONDITIONS: finds, inserts and deletes accounts
{
acct bank[1000]; // list of bank accounts
int numaccts; // total number of accounts
ifstream vault; // file with list of bank accounts
string command; // user request
int goal; // account number to find or delete
int loc; // subscript of account in bank list
acct account; // a bank account
float amount;
// read existing accounts from file
vault.open("accounts.txt");
numaccts=0;
vault >> bank[0].num >> bank[0].name >> bank[0].balance;
while (!vault.eof()) {
numaccts = numaccts + 1;
vault >> bank[numaccts].num >> bank[numaccts].name
>> bank[numaccts].balance;
}

// display dollar values with two digits to right of decimal point
cout << setiosflags(ios::fixed) << setprecision(2);

// get ready to process user commands
cout << "The bank is now open." ;
cout << endl << endl;

// loop to process user commands
cout << endl << "----------" << endl;
cout << "Enter a command (exit to stop): ";
cin >> command;
while (command != "exit") {

if (command == "find") { // FIND COMMAND
// get account to find
cout << "Enter account number: ";
cin >> goal;
// search for account
loc = find(bank, numaccts, goal);
// display account data or error message
if (loc >= 0) {
cout << "Account: " << bank[loc].num << "\t"
<< "Owner: " << bank[loc].name << "\t"
<< "Balance: " << bank[loc].balance << endl;
}
else {
cout << "Account " << goal <<" does not exist." << endl;
}
}

else if (command == "list") { // LIST COMMAND
cout << "List of accounts:" << endl;
for (loc=0; loc cout << "Account: " << bank[loc].num << "\t"
<< "Owner: " << bank[loc].name << "\t"
<< "Balance: " << bank[loc].balance << endl;
}
}

else if (command == "insert") { // INSERT COMMAND
cout << "Enter new account data: " << endl;
cout << "Account number: ";
cin >> account.num;
cout << "Account name: ";
cin >> account.name;
cout << "Account balance: ";
cin >> account.balance;
insert(bank, numaccts, account);
}
else if(command=="remove"){ //REMOVE COMMAND
cout<<"Enter the account number to remove: "< cin>>account.num;
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "< cin>>account.num;
}
remove(bank,numaccts,account);
}

else if(command=="transfer"){
cout<<"Enter first account number: "< cin>>account.num;
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "< cin>>account.num;
}
cout<<"Enter the balance to transfer: "< cin>>amount;
transfer(bank,numaccts,amount,account);
cout<<"Enter second account number: "< cin>>account.num;
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "< cin>>account.num;
}
amount=-1*amount;
transfer(bank,numaccts,amount,account);
}

else { // INVALID COMMAND
cout << "Invalid command." << endl;
}

cout << endl << "----------" << endl;
cout << "Enter a command (exit to stop): ";
cin >> command;
}

// exit command from user
cout << endl << "----------" << endl;
cout << "The bank is now closed.";
cout << endl << endl;

return 0;
}

int find(acct bank[ ], int numaccts, int goal)
//PURPOSE: search for a goal account in the list of accounts
//PRECONDITIONS: 0 <= numaccts, numaccts is number of account in
// list of accounts (i.e., bank)
//POSTCONDITIONS: returns the subscript of goal account or -1 if
// the goal account is not found
{
int k; // loop variable

//search the the goal account in list of accounts
for (k=0; k if (bank[k].num == goal) {
return k;
}
}

// didn't find goal account
return -1;
}

void insert(acct bank[ ], int &numaccts, acct newacct)
//PURPOSE: insert a new account into the account list
//PRECONDITIONS: 0 <= numaccts, numaccts is number of account
// in account list, newacct is a new account,
// account list is in order by account number
//POSTCONDITIONS: account list modified to add new account,
// order of account list is maintained
{
int k,j; // loop variables

// find place to put new account to maintain order of list
k=0;
while ((bank[k].num < newacct.num) && (k < numaccts)) {
k = k + 1;
}

// add new account to list
if (k == numaccts) { // goes at end of list
bank[numaccts] = newacct;
numaccts = numaccts + 1;
}
else { // goes in middle of list
// make room to add new account
for (j=numaccts; j>k; j--) {
bank[j] = bank[j-1];
}
// add new account to vacated spot in list
bank[k] = newacct;
numaccts = numaccts + 1;
}
}
void remove(acct bank[], int &numaccts, acct deleteacct)
//PURPOSE: remove an account from the account list
//PRECONDITION: 0<=numaccts, numaccts is # of accounts in list
// account list is in order by account number
//POSTCONDITION: returns list with one less account, which is the one
// that the user chose to remove
{
int x,y; //loop variables
y=find(bank, numaccts, deleteacct.num);
for(x=y; y<=numaccts; y++){
bank[y]=bank[y+1];
}
numaccts=numaccts-1;
}
void transfer(acct bank[], int &numaccts, float amount, acct account)
{
int i;
i=find(bank,numaccts,account.num);
bank[i].balance=bank[i].balance - amount;
}

Posted by poetry/maddoxtheconqueror at 12:01 AM CST
Post Comment | Permalink | Share This Post

View Latest Entries