我的数据文件包含以下要读入的数字
/*
111
100.00
200.00
50.00
222
200.00
300.00
100
*/
Run Code Online (Sandbox Code Playgroud)
但是当while循环读入customerNumber为100时应该是111,它也会得到其他所有错误的值.比如startsBalance如同阅读
//-9255963134931783000000000000000000000000.00
Run Code Online (Sandbox Code Playgroud)
其他一切似乎都在读同样的价值.我只是学习文件,所以任何帮助将不胜感激.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int customerNumber;
double beginningBalance, purchase, payments, financeCharge, endingBalance;
ifstream inputFile;
ofstream outputFile;
inputFile.open("BeginningBalance.dat");
cout<<"Cust No | Beginning Bal | Finance Charge | Purchases | Payments | Ending Balance"<<endl;
while (inputFile >> customerNumber);
{
inputFile >> beginningBalance;
inputFile >> purchase;
inputFile >> payments;
financeCharge = beginningBalance * .01;
endingBalance = beginningBalance + purchase + financeCharge - payments;
cout<<setw(5)<<customerNumber<<fixed<<setprecision(2)<<" "<<beginningBalance<<" "<<financeCharge<<" "<<purchase<<" "<<payments<<" "<<endingBalance<<endl;
}
system ("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试从while循环条件后删除分号,看看是否修复了它.
所以改变
while (inputFile >> customerNumber);
Run Code Online (Sandbox Code Playgroud)
至
while (inputFile >> customerNumber)
Run Code Online (Sandbox Code Playgroud)
它现在的方式是什么,它直到它从文件中吃掉所有数据,然后在里面做东西{ ... },并且你已经在EOF中执行的文件读取,所以它们不起作用.