How to display the beginning balance read from the data file?
Okay so everytime i run the code and creates an infinite loop, but within that infinite loop the balance is displaying this huge number:
Balance = $-92559631349317830736831783200707727132248687965119994463780864.00
when i need it to display
Balance = 2000
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Author:
// Source file:
// Description:
// Compiler used:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct records
{
int code;
double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream&);
void displayBal(records);
records getData(ifstream&);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);
//Global Constants
const double CHARGE = 10,
ATMFEE = 2;
int main()
{
//Variable Declarations
int transCode;
double balance,
transAmt;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
records trans;
ifstream inFile;
inFile.open("c:\\checkIn.dat");
displayTitle();
balance = getBegBal(inFile);
getData(inFile);
while (!inFile.eof())
{
trans = getData(inFile);
switch (trans.code)
{
case 1: balance = processCheck(balance, trans.amount); break;
case 2: balance = processDeposit(balance, trans.amount); break;
case 3: balance = processATM(balance, trans.amount); break;
}
displayBal(trans);
if (balance < 0)
balance = processSvcChg(balance);
getData(inFile);
}
return 0;
}
void displayTitle()
{
cout << "\n Check Register\n\n";
}
double getBegBal(ifstream& inFile)
{
//double bal;
records balance;
inFile >> balance.amount;
displayBal(balance);
return balance.amount;
}
void displayBal(records rec)
{
cout << "\t\tBalance = $" << setw(10) << rec.amount;
}
records getData(ifstream& inFile)
{
records rec;
inFile >> rec.code >> rec.amount;
return rec;
}
double processCheck(double bal, double amt)
{
records trans;
trans.amount = 0;
cout << "\n Check = " << setw(10) << trans.amount;
return (bal - amt);
}
double processDeposit(double bal, double amt)
{
records trans;
trans.amount = 0;
cout << "\n Deposit = " << setw(10) << trans.amount;
return (bal + amt);
}
double processATM(double bal, double amt)
{
records trans;
trans.amount = 0;
cout << "\n ATM = " << setw(10) << trans.amount;
bal = bal - amt;
displayBal(trans);
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
records trans;
trans.amount = 0;
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(trans);
return (bal);
}
|
also here is the data file
2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800
Last edited on
Topic archived. No new replies allowed.