2016-04-10 21 views
-2

Böylece her Tamam ben kod çalıştırmasına ve sonsuz döngü oluşturur, ancak bu sonsuz döngü içinde bakiye bu çok sayıda görüntülüyor:İşlev göstergesiBal'i nasıl düzeltirim?

Dengesi = $ -92559631349317830736831783200707727132248687965119994463780864,00 i Denge = görüntülemek için gereksinim duyduğunuzda 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); 
} 

de burada bunu kontrol edilmez

2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800 

cevap

0

okunan varsayalım veri dosyasıdır hatalar. while (!inFile.eof()) yerine while (inFile)'u deneyin. Gerçekten çalıştığından emin olmak için her okunmanın ardından assert(inFile)'u düşürmeyi düşünebilirsiniz.

İlgili konular