2011-11-22 44 views
1

大家好我只是在学习我的第一个编程语言,并且刚纠正了我的代码中的一个错误,只留下了另一个错误。谢天谢地,这是我需要解决的最后一个问题,但我自己也有点麻烦。错误的文件输出使用数组

该程序将读取与每个客户编号相关的客户编号和费用。然后,它将添加财务费用,输出所有费用,总计所有费用,并将每个客户的新余额以及其ID号码以相反的顺序保存。

beginningbalance.dat看起来像

111 
200.00 
300.00 
50.00 
222 
300.00 
200.00 
100.00 

和newbalances.dat应该像

222 
402.00 
111 
251.00 

除了不同的地方在把在文件写入循环 “count--”,我结束与

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