Lecture141 - Bank...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 1 -- 1 Computer Science I - Martin Hardwick Bank Simulation (1) #include <iostream> #include <iomanip> #include <string> #include <fstream> 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, int acctnum); This program simulates a bank by processing transactions for bank accounts. An array is used to hold the list of bank accounts. A bank account consists of an account number, the name of the person who owns it, and the current balance. we need a way to combine  these three pieces of data  into a single piece of  account data we will use a C++ struct  to do this
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 1 -- 2 Computer Science I - Martin Hardwick C++ structs Syntax: Example: Think of a struct as a way to combine heterogeneous data values together to form a new more complex data type. you can use this new data type to declare variables you can create arrays with this new data type Place struct definitions at the beginning of a program, after include lines and before functions and function prototypes. To access the components of a struct, use the dot operator. can be used anywhere an ordinary variable can be used struct <name> { <type> <name>; <type> <name>; . . . }; struct acct { int acctnum; string name; float balance; }; acct myAccount; myAccount.acctnum = 99999; myAccount.name = “Hardwick”; myAccount.balance = 10000;
Background image of page 2
Lecture 1 -- 3 Computer Science I - Martin Hardwick Bank Simulation (2) int main () //PURPOSE: simulate a small bank //PRECONDITIONS: existing  //     accounts in file account.txt //     in project folder //POSTCONDITIONS: finds,  inserts,  //     deletes, and lists accounts { // list of bank accounts acct  bank[1000]; // total number of accounts int numaccts; // file with list of bank accounts ifstream vault; // user request string command; // account number to find int goal; The main function creates the list of bank accounts and processes user commands to perform transactions. Assume that there will be no more than 1000 accounts. The existing accounts are read from a file at the start of the program. this creates the bank array  for the program to use User commands are typed as strings (e.g., find, insert, exit).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 1 -- 4 Computer Science I - Martin Hardwick Bank Simulation (3) // subscript of account in bank  int loc; // a bank account  acct account; // read existing accounts from file vault.open("accounts.txt"); numaccts=0; vault >> bank[0].num  >> bank[0].name  >> bank[0].balance;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/30/2012 for the course CSCI 101 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 15

Lecture141 - Bank...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online