2011-11-22 2 views
1

안녕하십니까. 첫 번째 프로그래밍 언어를 배우면서 방금 다른 오류가있는 코드 하나만 수정했습니다. 고맙게도이 문제는 해결해야 할 마지막 문제이지만 직접 해결하는 데 약간의 문제가 있습니다.배열을 사용하는 잘못된 파일 출력

프로그램은 각 고객 번호와 관련된 고객 번호와 비용을 읽는 것입니다. 그러면 금융 비용이 추가되고 모든 비용이 출력되며 모든 금액이 출력되고 각 고객의 신규 잔액이 파일의 ID 번호와 함께 역순으로 저장됩니다.

111 
200.00 
300.00 
50.00 
222 
300.00 
200.00 
100.00 

및 newbalances.dat가있는가 "count--"파일 쓰기 루프에서, 내가 끝낼 위치에 따라 제외

222 
402.00 
111 
251.00 

처럼 보일 것 같은

beginningbalance.dat 보인다 최대 함께

222 
402.00 
111 
251.00 
-858993460 
-92559631349317830000000000000000000000000000000000000000000000.00 

이러한 추가 값을 제거하려면 어떻게 해야할지 알 수 없습니다. 어떤 제안?

여기

#include <iostream> 
#include <fstream> 
#include <iomanip> 
using namespace std; 



int main() 
{ 
    int count = 0; 
    const int size = 100; 
    double customer_beg_balance, customer_purchases, customer_payments, finance_charge_cost, finance_charge = .01; 
    int customer_number[size]; 
    double new_balance[size]; 
    double total_beg_balances=0,total_finance_charges=0,total_purchases=0,total_payments=0,total_end_balances=0; 


    ifstream beginning_balance; 
    beginning_balance.open("beginningbalance.dat"); 

    while(beginning_balance>>customer_number[count]) 
    { 
     beginning_balance >> customer_beg_balance; 
     beginning_balance >> customer_purchases; 
     beginning_balance >> customer_payments; 

     finance_charge_cost = customer_beg_balance * finance_charge; 

     new_balance[count] = customer_beg_balance + finance_charge_cost + customer_purchases - customer_payments; 
     total_beg_balances+=customer_beg_balance; 
     total_finance_charges+=finance_charge_cost; 
     total_purchases+=customer_purchases; 
     total_payments+=customer_payments; 
     total_end_balances+=new_balance[count]; 

     cout<<fixed<<setprecision(2)<<setw(8)<<"Cust No "<<"Beg. Bal. "<<"Finance Charge "<<"Purchases "<<"Payments "<<"Ending Bal.\n" 
      <<customer_number[count]<<"  "<<customer_beg_balance<<"  "<<finance_charge_cost<<"  "<<customer_purchases<<"  "<<customer_payments<<"   "<<new_balance[count]<<endl; 


     count++;        
    } 

    cout<<"\nTotals  "<<total_beg_balances<<"  "<<total_finance_charges<<"  "<<total_purchases<<"  "<<total_payments<<"   "<<total_end_balances<<endl; 

    ofstream new_balance_file; 
    new_balance_file.open("NewBalance.dat"); 

    while(count >= 0) 
    { 
    count--; 
    new_balance_file <<customer_number[count]<<endl; 
    new_balance_file<< fixed<<setprecision(2)<<new_balance[count]<<endl; 

    } 


    new_balance_file.close(); 


system("pause"); 
     return 0; 
} 
+0

출력이 전적으로 보이지 않기 때문에 정의되지 않은 동작이 발생합니다. – moshbear

+0

moshbear : 정말요? 배열의 과거 한쪽 끝을 실행하는 것을 보았습니다 –

+0

다른 사람들을 괴롭히기 전에 코드를 디버그하려고합니다. – Walter

답변

3

귀하의 조건이

while(count >= 0) 
{ 
    count--; 
    new_balance_file <<customer_number[count]<<endl; 
    new_balance_file<< fixed<<setprecision(2)<<new_balance[count]<<endl; 

} 

count > 0 대신 count >= 0이어야한다 잘못 내 코드입니다.

이 방법은 숫자 n, n-1, ... 1을 반복하고 while 문이 시작될 때 줄이기 때문에 루프 내에서 조작 할 값은 정확히 n-1, n-2.... 0이됩니다. 코스는 이미 for 루프를 적용하고 당신이 그것을 사용할 수 있다면 그런데

는, 다음은 학업이

for(int i = count - 1; i >= 0; --i) 
{ 
    use i instead of count here. 
} 

희망이 도움이 행운처럼 더 적합!

+0

고맙습니다. – sircrisp