RUN THIS CODE THEN ANSWER THE FOLLOWING 1 HOW CAN I USE whil
RUN THIS CODE THEN ANSWER THE FOLLOWING:
1. HOW CAN I USE while(!inFile.eof()) IN THIS PROGRAM INSTEAD OF while (transCode != 0)?
2. HOW CAN I PRINT THE OUTPUT TO THE checkOut.dat FILE?
3. HOW CAN I DISPLAY THE NUMBER OF TRANSACTIONS IN THE FILE? (AS IN BY CREATING A COUNTER OF EVERY TRANSACTION).
4. THIS IS THE checkIn.dat file data
2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800 0 (note that 2000 is the beginning bal, 1,3,2 are transaction code folloed by actual amount)
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct record {
int code;
double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream& in);
void displayBal(double);
record getData(ifstream& in);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);
//Global Constants
const double CHARGE = 10;
const double ATMFEE = 2;
int main()
{
//Variable Declarations
int transCode;
double balance;
double transAmt;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
record trans;
ifstream inFile;
//open the file containing data if fie cant open disply error
inFile.open(\"C:\\\\Users\\\\pat\\\\Downloads\\\\checkIn.dat\");
if (inFile.fail())
{
cout << \" Error unable to open checkIn.dat file \" << endl;
exit(1);
}
//display the beggining balance
displayTitle();
balance = getBegBal(inFile);
cout << \" Beginning balance: = $ \" << balance << endl;
trans = getData(inFile);
transCode = trans.code;
transAmt = trans.amount;
// this gets and displays balance and all transactions
while (transCode != 0)
{
switch (transCode)
{
case 1: balance = processCheck(balance, transAmt); break;
case 2: balance = processDeposit(balance, transAmt); break;
case 3: balance = processATM(balance, transAmt); break;
}
displayBal(balance);
if (balance < 0)
balance = processSvcChg(balance);
trans = getData(inFile);
transCode = trans.code;
transAmt = trans.amount;
}
//output file
ofstream outFile;
outFile.open(\"C:\\\\Users\\\\pat\\\\Desktop\\\\checkOut.dat\");
cout << \" \ \ Your final balance is: $ \" << balance << endl;
inFile.close();
outFile.close();
cin.get();
return 0;
}
//displays the title
void displayTitle()
{
cout << \"\ Check Register\ \";
cout << \" ...........................\" << endl;
}
//gets starting balance
double getBegBal(ifstream &in)
{
double bal;
in >> bal;
return bal;
}
//displays new balance
void displayBal(double x)
{
cout << \"\\t\\tBalance = $\" << setw(10) << x;
}
// gets data from file
record getData(ifstream &in)
{
int code;
double amt = 0;
in >> code;
if (code > 0)
{
in >> amt;
}
record get;
get.amount = amt;
get.code = code;
return get;
}
//calculat check amount
double processCheck(double bal, double amt)
{
cout << \"\ Check = \" << setw(10) << amt;
return (bal - amt);
}
//calculate deposit
double processDeposit(double bal, double amt)
{
cout << \"\ Deposit = \" << setw(11) << amt;
return (bal + amt);
}
//calculate atm
double processATM(double bal, double amt)
{
cout << \"\ ATM = \" << setw(11) << amt;
bal = bal - amt;
displayBal(bal);
bal = bal - ATMFEE;
cout << \"\ ATM Fee = \" << setw(11) << ATMFEE;
return (bal);
}
//calculte fees
double processSvcChg(double bal)
{
cout << \"\ Service chg =\" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(bal);
return (bal);
}//End of program
Solution
1) used while(!inFile.eof()) in the program
2) output the results onto file ( replace every cout with fileoutput )
3) added no of transaction
------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct record {
int code;
double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream& in);
void displayBal(double);
record getData(ifstream& in);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);
//Global Constants
const double CHARGE = 10;
const double ATMFEE = 2;
//output file
ofstream outFile;
int main()
{
//Variable Declarations
int transCode;
double balance;
double transAmt;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
record trans;
ifstream inFile;
int noTrans=0;
//open the file containing data if fie cant open disply error
inFile.open(\"C:\\\\Users\\\\Admin\\\\Desktop\\\\Chegg\\\\programs\\\\Programs\\\\Programs\\\\checkIn.dat\");
outFile.open(\"C:\\\\Users\\\\Admin\\\\Desktop\\\\Chegg\\\\programs\\\\Programs\\\\Programs\\\\checkOut.dat\");
//outFile.precision(2);
if (inFile.fail())
{
cout << \" Error unable to open checkIn.dat file \" << endl;
exit(1);
}
//display the beggining balance
displayTitle();
balance = getBegBal(inFile);
outFile << \" Beginning balance: = $ \" << balance << endl;
//cout << \" Beginning balance: = $ \" << balance << endl;
trans = getData(inFile);
transCode = trans.code;
transAmt = trans.amount;
// this gets and displays balance and all transactions
//while (transCode != 0)
while(!inFile.eof())
{
switch (transCode)
{
case 1:
balance = processCheck(balance, transAmt);
noTrans++;
break;
case 2:
balance = processDeposit(balance, transAmt);
noTrans++;
break;
case 3:
balance = processATM(balance, transAmt);
noTrans++;
break;
}
displayBal(balance);
if (balance < 0)
{
balance = processSvcChg(balance);
}
trans = getData(inFile);
transCode = trans.code;
transAmt = trans.amount;
}
outFile << \" \ \ Your final balance is: $ \" << balance << endl;
outFile << \" \ \ No of transactionss: \" << noTrans << endl;
inFile.close();
outFile.close();
cin.get();
return 0;
}
//displays the title
void displayTitle()
{
outFile << \"\ Check Register\ \";
outFile << \" ...........................\" << endl;
}
//gets starting balance
double getBegBal(ifstream &in)
{
double bal;
in >> bal;
return bal;
}
//displays new balance
void displayBal(double x)
{
outFile << \"\\t\\tBalance = $\" << setw(10) << x;
}
// gets data from file
record getData(ifstream &in)
{
int code;
double amt = 0;
in >> code;
if (code > 0)
{
in >> amt;
}
record get;
get.amount = amt;
get.code = code;
return get;
}
//calculat check amount
double processCheck(double bal, double amt)
{
outFile << \"\ Check = \" << setw(10) << amt;
return (bal - amt);
}
//calculate deposit
double processDeposit(double bal, double amt)
{
outFile << \"\ Deposit = \" << setw(11) <<amt;
return (bal + amt);
}
//calculate atm
double processATM(double bal, double amt)
{
outFile << \"\ ATM = \" << setw(11) << amt;
bal = bal - amt;
displayBal(bal);
bal = bal - ATMFEE;
outFile << \"\ ATM Fee = \" << setw(11) << ATMFEE;
return (bal);
}
//calculte fees
double processSvcChg(double bal)
{
outFile << \"\ Service chg =\" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(bal);
return (bal);
}//End of program





