好的,所以每次我运行代码并创建一个无限循环,但在这个无限循环中,余额显示这个巨大的数字:
余额= $ -92559631349317830736831783200707727132248687965119994463780864.00 当我需要它显示 余额= 2000
// 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);
}
这里也是从
中读取的数据文件2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800