-2

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

// 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 it is suppose to read from

2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800 
Bart Alen
  • 69
  • 8

1 Answers1

0

You are not checking for errors. Try while (inFile) instead of while (!inFile.eof()). You might want to consider dropping some assert(inFile) after every read to make sure it actually worked.

nwp
  • 8,897
  • 2
  • 32
  • 67