2013-02-17 3 views
0

굿 데이,dat 파일을 잘못 읽는 루프

루프가 dat 파일을 읽지 만 루프가 데이터를 잘못 읽습니다. 내 DAT 파일은 출력이

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 

해야 다음 데이터

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 : 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

답변

2

첫 번째 문제는 if 블록입니다.

if(Xxx) 
    thisHappensOnlyIfXxxIsTrue 
thisAlwaysHappens 

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


if(Xxx) { 
    thisHappensOnlyIfXxxIsTrue 
    thisDependsOnXxxToo 
} 
thisAlwaysHappens 

다음 문제

당신이 때마다 당신의 변수 루프 다시 시작을 삭제하지 않을 것입니다 ... 코드 또는 block.so의 라인 - C++에서는 '는 경우는'문은 immeidately 다음과 같은 일을 영향을줍니다. 당신은 당신이 다시 사용하지 않을 것을 TEH 마지막 루프 반복에서 값을 보장하기 위해, 루프의 시작 (또는 끝)에서

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

를 삽입해야합니다.

+0

삭제 주셔서 감사합니다 작동하는 부분. 그러나 나는 아직도 필요한 것을 얻지 못하고있다. 반복되는 각 글자와 동일한 출력. 제발 출력 부분을 도와주세요. – user1291092

+0

언제나 편지를 지울 필요가있을 것입니다 - 원인이 아니더라도 좋은 습관입니다. 또한 코드가 지금 보이는 방식을 나타 내기 위해 원본 게시물을 편집해야합니다. 코드를 심령으로 읽고 의견을 말하기가 매우 어렵습니다. – atk

+0

나는 여전히 출력에 문제가 있습니다. 위와 같이 출력 할 수 없습니다. 내가 atk에서 위의 라인을 추가했습니다. – user1291092

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()); 
관련 문제