[英]C++ Reading in wrong values from a file
我的數據文件包含以下要讀取的數字
/*
111
100.00
200.00
50.00
222
200.00
300.00
100
*/
但是,當while循環將customerNumber讀為100時(應為111)時,它也會得到其他所有錯誤的值。 如開始平衡閱讀為
//-9255963134931783000000000000000000000000.00
其他所有內容似乎都在讀取相同的值。 我只是在學習文件,因此非常感謝您的幫助。
#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;
}
嘗試從while循環條件后刪除分號,看看是否可以解決此問題。
所以改變
while (inputFile >> customerNumber);
至
while (inputFile >> customerNumber)
現在的方式是,它什么也沒做,直到它吞噬了文件中的所有數據, 然后在{ ... }
,文件在其中讀取的內容已經在EOF上,因此它們無法工作。
文件開頭的/ *破壞了您的數據。 在讀取客戶編號之前,請先讀兩個垃圾字符,以刪除/ *或為其添加帳戶。
另外,在while循環語句后還需要刪除一個分號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.