从文件中读取错误的值
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,它也得到了其他所有错误的值。例如beginningBalance读取为
//-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循环语句后面还有一个分号需要删除。
相关文章:
- 分段错误 - 读取初始化指针的数组
- 在指向函数中读取变量时出现分割错误
- 读取某些文件时出现分段错误,似乎与文件大小无关
- 从对象C++中的文件读取内容时出现分段错误
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 可能的 ComputeCPP SYCL 错误读取嵌套缓冲区
- Eigen SparseLU解决错误读取访问冲突,这>m_sup_to_col被0x111011101110112
- 访问违规读取位置和错误读取字符串的错误
- libpng 错误:读取错误
- 错误读取字符到矩阵
- 分段错误读取 JSON 文件
- 斯特托克获取分段错误读取文件
- ifstream 初始化错误 - 读取字符时出错
- 读取错误-读取时无限
- 异常错误:读取位置0xDDDDDDDD时发生访问冲突
- 如何修复此错误读取位置0x00000008时发生访问冲突
- GDB:<错误读取变量>,带有 C++ 向量
- 找不到错误 读取大小 4 瓦尔格林德无效
- 分割错误读取文件
- libpng错误:读取错误(Visual Studio 2010)