I'm having trouble with arrays. In the following program, I can open the file and access the data ok, but when it writes back to the outfile it's messing up the data. The text file is an attachment.
Code:
//customer class definition
# include <iostream.h>
# include <fstream.h>
# include <iomanip.h>
class cust
{
private:
char FirstNameId;
char LastNameId;
int num_of_recs;
int ChkAcctId;
double ChkAcctBal;
char Overdraft_flg;
int SavAcctId;
double SavAcctBal;
double SvIntrst;
int PIN;
public:
void setFirstNameId (char FirstName) {FirstNameId = FirstName;};
void setLastNameId (char LastName) {LastNameId = LastName;};
void setChkActId (int ChkId) {ChkAcctId = ChkId;};
void setChkAcctBal (double ChkBal) {ChkAcctBal = ChkBal;};
void setOverdraft_flg(char Overd_flg) {Overdraft_flg = Overd_flg;};
void setSavAcctId (int SavId) {SavAcctId = SavId;};
void setSavAcctBal (double SavBal) {SavAcctBal = SavBal;};
void setSvIntrst (double SvIntrstRate) {SvIntrst = SvIntrstRate;};
void setPIN (int PIN_num) {PIN = PIN_num;};
void displayBalance ();
void depos (int selec, double depAmnt);
void withdrawl (int selec, double withdrawalAmnt);
void transferFunds (int transdirect, double transAmnt);
int Cust_pin () {return PIN;};
char getFirstNameId() {return FirstNameId;};
char getLastNameId() {return LastNameId;};
int getChkActId () {return ChkAcctId;};
double getChkAcctBal () {return ChkAcctBal;};
char getOverdraft_flg () {return Overdraft_flg;};
int getSavAcctId () {return SavAcctId;};
double getSavAcctBal () {return SavAcctBal;};
double getSvIntrst () {return SvIntrst;};
};
//This function is used to display the balances when called.
void cust::displayBalance()
{
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout <<endl<< "\t\t\tChecking Account Balance : $"<<ChkAcctBal<<endl;
cout << "\t\t\tSavings Account Balance : $"<<SavAcctBal<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
return ;
}
//This function is designed to process deposits to accounts. It recieves
//the targeted account of transaction and the deposit amount. It calculates
//the propare balance and sets new balance in array element. A message is
//displayed indicating the deposit has been made
void cust::depos(int selec, double deposit)
{
if (selec == 2)
{
ChkAcctBal = ChkAcctBal + deposit;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout << "\t\t\tDeposited to Checking: $" <<deposit<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}//end if
if (selec == 1)
{
SavAcctBal = SavAcctBal + deposit;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout << "\t\t\tDeposited to Savings: $"<<deposit<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}//end if
}
//This function handles all withdrawls from accounts. It determines if withdrawal
//will be permitted by checking if overdraft protection is 'Y' and balances on
//checking accounts will be less then 0. If withdrawal is from savings account
//function will determine whether it will result in a negative balance. If so
//withdrawal will not be permitted. Function then updates the array and displays
//an appropriate message.
void cust::withdrawl(int selec, double withdrawalAmnt)
{
if (selec == 1 && (SavAcctBal - withdrawalAmnt >= 0) )
{
SavAcctBal = SavAcctBal - withdrawalAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout <<endl<<"\t\t\tWithdrawal Successfull : $"<<withdrawalAmnt<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if (selec == 1 && (SavAcctBal - withdrawalAmnt < 0) )
{
cout << "\t\t\tCannot Complete Transaction"<<endl<<endl;
cout <<endl<< "\t\t\t Insufficient Funds"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}//end if
if (selec == 2 && (ChkAcctBal - withdrawalAmnt >= 0))
{
ChkAcctBal = ChkAcctBal - withdrawalAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout <<endl<<"\t\t\tWithdrawal Successfull : $"<<withdrawalAmnt<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if (selec == 2 && (ChkAcctBal - withdrawalAmnt < 0) && Overdraft_flg == 'Y')
{
ChkAcctBal = ChkAcctBal - withdrawalAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout <<endl<<"\t\t\tWithdrawal Successfull : $"<<withdrawalAmnt<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if (selec == 2 && (ChkAcctBal - withdrawalAmnt < 0) && Overdraft_flg == 'N')
{
cout <<"\t\t\tCannot Complete Transaction"<<endl<<endl;
cout <<endl<<"\t\t\t Insufficient Funds"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}//endif
}
//This function handles all transfers between accounts. It determines if transfer
//will be permitted by checking if overdraft protection is 'Y' and balances on
//checking accounts will be less then 0. If transfer is from savings account
//function will determine whether it will result in a negative balance. If so
//trnasfer will not be permitted. Function then updates the array and displays
//an appropriate message.
void cust::transferFunds (int transdirect, double transAmnt)
{
if ( transdirect == 1 && (SavAcctBal - transAmnt >= 0))
{
SavAcctBal = SavAcctBal - transAmnt;
ChkAcctBal = ChkAcctBal + transAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout << "\t\t\t Transfer Successful"<<endl<<endl;
cout << "\t\t\t$"<<transAmnt<<" Transferred into Checking"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if (transdirect == 1 && (SavAcctBal - transAmnt < 0))
{
cout <<"\t\t\tCannot Complete Transaction"<<endl<<endl;
cout <<"\t\t\t Insufficient Funds"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}//end if
if ( transdirect == 2 && (ChkAcctBal - transAmnt >= 0))
{
ChkAcctBal = ChkAcctBal - transAmnt;
SavAcctBal = SavAcctBal + transAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout << "\t\t\t Transfer Successful"<<endl<<endl;
cout << "\t\t\t$"<<transAmnt<<" Transferred into Savings"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if ( transdirect == 2 && (ChkAcctBal - transAmnt < 0) && Overdraft_flg == 'Y')
{
ChkAcctBal = ChkAcctBal - transAmnt;
SavAcctBal = SavAcctBal + transAmnt;
cout <<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint);
cout << "\t\t\t Transfer Successful"<<endl<<endl;;
cout << "\t\t\t$"<<transAmnt<<" Transferred into Savings"<<endl<<endl;
cout << "\t\t\tOverdraft Proctection Has Been Used"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
else if ( transdirect == 2 && (ChkAcctBal - transAmnt < 0) && Overdraft_flg == 'N')
{
cout << "\t\t\tCannot Complete Transaction"<<endl<<endl;
cout << "\t\t\t Insufficient Funds"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
}
}
//atm class definition
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
class atm
{
private:
int pin;
int transtype;
double amnt;
int transDirec;
int accnt;
public:
void setpin();
int getpin() {return pin;};
int displayMenu();
int getAcct();
double getAmnt();
int getDirection();
void clear();
void incorrectPinDisp(int cntr);
char ContinueDisp();
void transCompleteDisp();
void atmGoodbye();
};
//this function displays the greeting message and prompts for the users
//PIN number which is stored in the class.
void atm::setpin()
{
cout <<endl<<endl<<endl<<endl<<endl;
cout <<"\t\t\t------------------------"<<endl;
cout <<"\t\t\t- Welcome To -"<<endl;
cout <<"\t\t\t- Latona Savings Bank -"<<endl;
cout <<"\t\t\t------------------------"<<endl<<endl;
cout <<"\t\t\tEnter your Pin Number: ";
cin >>pin;
}
//This function is called once the PIN number is verified against the
//customer array. It prompts user to select a transaction and returns
//the value back to main. It is designed to keep prompting user until
//a valid selection is made.
int atm::displayMenu()
{
int selection = 0;
while (selection > 5 || selection < 1)
{
cout <<endl<< "\t\t\t1 - Display Balance"<<endl;
cout << "\t\t\t2 - Deposit"<<endl;
cout << "\t\t\t3 - Withdrawl"<<endl;
cout << "\t\t\t4 - Transfer Funds"<<endl;
cout << "\t\t\t5 - Exit"<<endl;
cout << "\t\t\t--------------------"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
cout << "\t\t\tSelect a Transaction: ";
cin >>selection;
cout <<endl<<endl<<endl<<endl;
if (selection == 1 || selection == 2 || selection == 3
|| selection == 4 || selection == 5)
{
}
else
{
cout <<endl<< "\t\t\tInvalid Selection"<<endl;
cout << "\t\t\tSelect 1 through 5 : ";
cin >> selection;
}//end if
}//end while
transtype = selection;
return selection;
}
//This function is designed to get the target account for the selected
//transaction and return the value back to main. It will Prompt for
//target account and validate selection. If selection is not correct
//it will continue to prompt until valid selection is made.
int atm::getAcct()
{
int acnttype = 0;
while (acnttype < 1 || acnttype > 2)
{
cout <<endl<< "\t\t\t1 - Savings"<<endl;
cout << "\t\t\t2 - Checking"<<endl;
cout << "\t\t\t-------------"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
cout << "\t\t\tSelect Account : ";
cin >>acnttype;
cin.ignore(100, '\n');
if (acnttype == 1 || acnttype == 2)
{
}
else
{
cout <<endl<< "\t\t\tInvalid Selection"<<endl;
cout << "\t\t\tEnter 1 or 2 : ";
cin >>acnttype;
cin.ignore(100, '\n');
}//end if
}//end while
accnt = acnttype;
return acnttype;
}
//This function prompts for an amount for the transaction and returns the
//value back to main for further processing.
double atm::getAmnt()
{
double amount = (float) 0.0;
cout <<"\t\t\tPlease Enter Transaction Amount"<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout << "\t\t\tAmount : $";
cin >>amount;
amnt = amount;
return amount;
}
//This function prompts the user for a transaction direction to be used in
//determining which direction a transfer should go. User is prompted until
//a valid selection is made.
int atm::getDirection()
{
int direct = 0;
while (direct < 1 || direct > 2)
{
cout << "\t\t\t1 - Savings to Checking"<<endl<<endl;
cout << "\t\t\t2 - Checking to Savings"<<endl<<endl;
cout << "\t\t\t-----------------------"<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl;
cout << "\t\t\tEnter a Selection : ";
cin >>direct;
cin.ignore(100, '\n');
if (direct == 1 || direct == 2)
{
}
else
{
cout <<endl<< "Re-Enter selection 1 or 2"<<endl<<endl;
}//end if
}//end while
transDirec = direct;
return direct;
}
//This function is called when an incorrect PIN number is entered. If
//an incorrect number is entered three times it will inform the user
//that they have entered three incorrect PIN numbers and program will
//terminate.
void atm::incorrectPinDisp(int cntr)
{
if (cntr < 3)
{ cout <<endl<<endl<<endl<<endl<<endl;
cout <<"\t\t\t------------------------"<<endl;
cout <<"\t\t\t- Welcome To -"<<endl;
cout <<"\t\t\t- Latona Savings Bank -"<<endl;
cout <<"\t\t\t------------------------"<<endl<<endl;
cout <<endl<< "\t\t Incorrect PIN, Please Re-Enter :";
}
else
{
cout << "\t\t\tYou Have Entered 3 Incorrect Pin Numbers\n"<<endl;
cout << "\t\t\t Please Contact Your Bank"<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}//end if
}
//this function Prompts the user for a yes or no answer to continue and returns
//the value back to main to be evaluated.
char atm::ContinueDisp()
{
char cont = 'Y';
cout <<endl<<"\t\t\tWould You Like Another Transaction? Y/N : ";
cin >>cont;
cin.ignore(100,'\n');
return cont;
}
//This function is called when the user terminates the session by choosing
// 'n' to continue or option 5 on the main menu.
void atm::atmGoodbye()
{
atm::clear();
cout <<endl<<endl<<endl<<endl<<endl;
cout <<"\t\t\t------------------------"<<endl;
cout <<"\t\t\t- Thank You -"<<endl;
cout <<"\t\t\t- For Banking With Us -"<<endl;
cout <<"\t\t\t- Latona Savings Bank -"<<endl;
cout <<"\t\t\t------------------------"<<endl<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl;
}
//This function is used to clea the screen when called.
void atm::clear()
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl
<<endl<<endl<<endl<<endl<<endl;
}
# include <iostream.h>
# include <fstream.h>
# include <stdlib.h>
//# include "atm.h"
//# include "customer.h"
void main()
{
int menuSelec = 0;
int pinHolder = 0;
int ArrayNum = 1;
int cntr = 1;
int selection = 0;
int transferDirec = 0;
double depAmnt = (float) 0.0;
double withdrawlAmnt = (float) 0.0;
double transferAmnt = (float) 0.0;
char ContSw = 'Y';
//opens infile, checks if opened correctly.
//if open fails message is displayed.
ifstream custFile;
custFile.open("bank.txt");
if (custFile.fail())
{
cout << "Unable to Access Bank Records"<<endl;
exit(1);
};//end if
//opens output file, checks if opened correctly.
//if opens fails message is displayed.
ofstream custUpdate;
custUpdate.open("recordUpdate.txt");
if (custUpdate.fail())
{
cout << "Error Opening Outfile"<<endl;
exit(1);
};//end if
int numrecs = 0;
custFile >> numrecs;
cust custArray[50];
atm atm;
char FirstName[25];
char LastName[25];
int ChkId;
double ChkBal;
char Overd_flg;
int SavId;
double SavBal;
double SvIntrstRate;
int PIN_num;
//Arry is filled with data from the infile. when finished
//infile is closed.
int x = 0;
int y = 0;
while (custFile >> FirstName >> LastName >>ChkId>> ChkBal >>Overd_flg >> SavId
>> SavBal >> SvIntrstRate >> PIN_num )
{
custArray[x].setFirstNameId(FirstName[y]);
custArray[x].setLastNameId(LastName[y]);
custArray[x].setChkActId(ChkId);
custArray[x].setChkAcctBal(ChkBal);
custArray[x].setOverdraft_flg(Overd_flg);
custArray[x].setSavAcctId(SavId);
custArray[x].setSavAcctBal(SavBal);
custArray[x].setSvIntrst(SvIntrstRate);
custArray[x].setPIN(PIN_num);
x++;
y++;
}//endwhile
custFile.close();
//main function calls atm class to prompt user for pin
//pin is then returned and vallidated against the customer
//class array using the while loop below.
atm.setpin();
atm.clear();
pinHolder = atm.getpin();
x = 0;
while (cntr < 4)
{
for ( x = 0; x < numrecs; x++)
{
if (pinHolder == custArray[x].Cust_pin())
{
ArrayNum = x;
cntr = 4;
break;
}//end if
}//end for
if ( x == numrecs)
{
if (cntr < 3)
{
atm.incorrectPinDisp(cntr);
cin >> pinHolder;
atm.clear();
cntr++;
}
else
{
atm.incorrectPinDisp(cntr);
exit(0);
}//end if
}//end if
}//end while
//Once Pin Number is validated, a do-while loop is used to call the
//atm classes menu function to determine what transactions the
//user would like to perform. A switch statement determines what
//functions will be called.
do
{
if (toupper(ContSw) == 'Y')
{
menuSelec = atm.displayMenu();
}
else if (toupper(ContSw) == 'N' || menuSelec == 5)
{
atm.atmGoodbye();
break;
}
switch (menuSelec)
{
case 1:
atm.clear();
custArray[ArrayNum].displayBalance();
ContSw = atm.ContinueDisp();
atm.clear();
break;
case 2:
atm.clear();
selection = atm.getAcct();
atm.clear();
depAmnt = atm.getAmnt();
atm.clear();
custArray[ArrayNum].depos( selection, depAmnt);
ContSw = atm.ContinueDisp();
atm.clear();
break;
case 3:
atm.clear();
selection = atm.getAcct();
atm.clear();
withdrawlAmnt = atm.getAmnt();
atm.clear();
custArray[ArrayNum].withdrawl( selection, withdrawlAmnt);
ContSw = atm.ContinueDisp();
atm.clear();
break;
case 4:
atm.clear();
transferDirec = atm.getDirection();
atm.clear();
transferAmnt = atm.getAmnt();
atm.clear();
custArray[ArrayNum].transferFunds (transferDirec, transferAmnt);
ContSw = atm.ContinueDisp();
atm.clear();
break;
case 5:
atm.atmGoodbye();
}//end case
}while (menuSelec != 5);
//the following 'if' statement is used to evaluate the continue switch
//and case selection. if conditions are true the 'for' loop writes the
//updated array to the outfile and the outfile is closed. Program then
//terminates.
if (toupper (ContSw) == 'N' || menuSelec == 5)
{
custUpdate <<numrecs<<endl<<endl;
for ( x = 0; x < numrecs; x++)
{ FirstName[y] = custArray[x].getFirstNameId();
LastName[y] = custArray[x].getLastNameId();
ChkId = custArray[x].getChkActId();
ChkBal= custArray[x].getChkAcctBal();
Overd_flg = custArray[x].getOverdraft_flg();
SavId = custArray[x].getSavAcctId();
SavBal = custArray[x].getSavAcctBal();
SvIntrstRate = custArray[x].getSvIntrst();
PIN_num = custArray[x].Cust_pin();
custUpdate <<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
custUpdate <<FirstName << " " << LastName << " " << ChkId <<" "<<ChkBal<<" "<<Overd_flg<<" "<<SavId
<<" "<<SavBal<<" "<<SvIntrstRate<<" "<<PIN_num<<endl<<endl;
}//end for
custUpdate.close();
}//end if
}//end of main function