使用数组输出错误的文件
Incorrect file output using arrays
大家好,我刚刚学习我的第一个编程语言,刚刚纠正了我的代码中的一个错误,只是留下了另一个错误。谢天谢地,这是我需要解决的最后一个问题,但我自己做起来有点麻烦。
程序将读取客户号码和与每个客户号码相关的费用。然后,它将添加财务费用,输出所有费用,合计所有内容,并将每个客户的新余额与他们的ID号以相反顺序保存到文件中。
beginningbalance.dat看起来像
111
200.00
300.00
50.00
222
300.00
200.00
100.00
和newbalance .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;
}
您的情况不对
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
。
这样count将遍历值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.
}
希望这对你有帮助,祝你学习顺利!
相关文章:
- C++系统找不到指定的文件错误
- C++语法头文件错误
- C++生成文件错误"implicit entry/start for main executable"
- C++/生成文件错误:未定义对"main"的引用
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- C++程序不制作文件(错误)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- Qt .exe文件错误
- c++ 添加具有写入文件错误的文本变量
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 写入文本文件错误,QT
- C++写入文件错误
- Windows Subsystem for Linux (WSL) 下的间歇性随机"找不到文件"错误
- 犰狳读取MAT文件错误
- 无法求解FullPathForFilename:COCOS2DX中可能缺少文件错误
- C Wininet FTP列出了许多文件错误
- 生成文件错误 - 找不到文件 - *.cpp