2013-02-17 70 views
0

Goodday,錯誤地讀取dat文件

我有一個循環讀取dat文件,但循環讀取的數據不正確。我的dat文件包含以下數據

46780976 3750.40 
W 250.00 
D 1200.00 
W 75.00 
W 375.00 
D 1200.00 
I 5.50 
W 400.00 
W 600.00 
D 450.50 
W 35.65 

輸出應該

Account number: 46780976 
Opening balance: R3750.40 

Transaction Amount Balance Bank costs 
Withdrawal  250.00 4000.00 
Deposit  1200.00Ct 2800.00 
Withdrawal  75.00 2725.00 
Withdrawal  1375.00 1350.00 
Deposit  1200.00Ct 1550.00 
Interest   5.50 1555.50 
Withdrawal  400.00 1155.50 
Withdrawal  600.00 555.50  25.00 
Deposit   450.00Ct 1005.50 
Withdrawal  35.65 969.85 
Banking costs 25.00 969.60 

Closing balance: R969.60 

我得到以下

Account number : 46780976 

Opening Balance : R 3750.4 

Transaction  Amount   Balance   Bank Costs 


Withdrawal   250   3500.4 
Deposit   0 Ct   3500.4 
Interest    0   3500.4 

Withdrawal   250   3225.4 
Deposit   1200 Ct   4425.4 
Interest    0   4425.4 

Withdrawal   75   4325.4 
Deposit   1200 Ct   5525.4 
Interest    0   5525.4 

Withdrawal   375   5125.4 
Deposit   1200 Ct   6325.4 
Interest    0   6325.4 

Withdrawal   375   5925.4 
Deposit   1200 Ct   7125.4 
Interest    0   7125.4 

Withdrawal   375   6725.4 
Deposit   1200 Ct   7925.4 
Interest    5.5   7930.9 

Withdrawal   400   7505.9 
Deposit   1200 Ct   8705.9 
Interest    5.5   8711.4 

Withdrawal   600   8086.4 
Deposit   1200 Ct   9286.4 
Interest    5.5   9291.9 

Withdrawal   600   8666.9 
Deposit   450.5 Ct   9117.4 
Interest    5.5   9122.9 

Withdrawal   35.65   9062.25 
Deposit   450.5 Ct   9512.75 
Interest    5.5   9518.25 

Withdrawal   35.65   9457.6 
Deposit   450.5 Ct   9908.1 
Interest    5.5   9913.6 
Bank Costs   25   9888.6   25 


Closing Balance : R 9888.6 
Press any key to continue . . . 

我的代碼:

do { 
    in_stream >> letter; 

if (letter == 'W') 

    in_stream >> withdrawal; 
    openBalance -= withdrawal; 
    cout << "\nWithdrawal" << "   "<< withdrawal << "   " << openBalance << endl; 
    out_stream << withdrawal; 

if (letter == 'D') 

    in_stream >> deposit; 
    openBalance += deposit; 
    cout << "Deposit" <<"   "<< deposit <<" Ct" <<"   " << openBalance << endl; 
    out_stream << deposit; 


if (letter == 'I') 

    in_stream >> interest; 
    openBalance += interest; 
    cout << "Interest" <<"    "<< interest <<"   " << openBalance << endl; 
    out_stream << interest; 

if (openBalance < 1000) 
    cout << "Bank Costs" <<"   "<< bankCost <<"   " << openBalance <<"   " << bankCost << endl; 
    openBalance -= bankCost; 
    out_stream << bankCost; 

} while(!in_stream.eof()); 

請點我正確的方向

感謝

+0

與您的錯誤無關,但會使您的代碼更具可讀性:['std :: setw'](http://en.cppreference.com/w/cpp/io/manip/setw) – us2012 2013-02-17 01:56:03

回答

2

你的第一個問題是你的if塊。在C++中,「如果」語句影響了immeidately以下的事情 - 代碼或block.so行...

if(Xxx) 
    thisHappensOnlyIfXxxIsTrue 
thisAlwaysHappens 

if(Xxx) 
    thisHappensOnlyIfXxxIsTrue 
    thisAlwaysHappens // even though it's indented 


if(Xxx) { 
    thisHappensOnlyIfXxxIsTrue 
    thisDependsOnXxxToo 
} 
thisAlwaysHappens 

接下來的問題是,你不能每次清除變量的循環重新開始。您需要在循環的開始(或結束)插入一個

do { 
    deposit = 0; 
    interest = 0; 
    withdrawal = 0; 

以確保您不會重複使用最後一次循環迭代中的值。

+0

感謝您的清除部分工作。但我仍然沒有得到所需的輸出。每個字母一遍又一遍地重複輸出。請幫助我輸出部分。 – user1291092 2013-02-17 19:28:22

+0

您可能需要每次都清除信件 - 即使它不是原因,這是一個很好的做法。您還應該編輯原始帖子,指出代碼現在的樣子 - 很難精確地讀取您的代碼並提供評論:) – atk 2013-02-17 20:23:43

+0

我仍然遇到輸出問題,無法按照上述方式獲取輸出。我從atk添加了上面的代碼。 – user1291092 2013-02-19 17:36:42

1

由於第一行是:

46780976 3750.40 

這條線將通過,但不想要的結果:

in_stream >> letter; 

假設當然是letter的類型是char

EDIT

// You need to read in the initial values here 

do { 
    in_stream >> letter; 

if (letter == 'W') 
{ 
    in_stream >> withdrawal; 
    openBalance -= withdrawal; 
    cout << "\nWithdrawal" << "   "<< withdrawal << "   " << openBalance << endl; 
    out_stream << withdrawal; 
} 

if (letter == 'D') 
{ 
    in_stream >> deposit; 
    openBalance += deposit; 
    cout << "Deposit" <<"   "<< deposit <<" Ct" <<"   " << openBalance << endl; 
    out_stream << deposit; 
} 

if (letter == 'I') 
{ 

    in_stream >> interest; 
    openBalance += interest; 
    cout << "Interest" <<"    "<< interest <<"   " << openBalance << endl; 
    out_stream << interest; 
} 

if (openBalance < 1000) 
{ 
    cout << "Bank Costs" <<"   "<< bankCost <<"   " << openBalance <<"   " << bankCost << endl; 
    openBalance -= bankCost; 
    out_stream << bankCost; 
    } 

} while(!in_stream.eof());