C++ 向量函数程序问题
输入文件与读取代码不匹配。您显示的文件中有9个值,但您的代码试图只读取8个值 当C++ 向量函数程序问题,c++,C++,输入文件与读取代码不匹配。您显示的文件中有9个值,但您的代码试图只读取8个值 当readIn()到达此代码时: Alan WakeField IT GUY T2034 40 15 1 Hourly 0.00 它试图读取一个double,但是文件每小时都有Hourly,因此读取失败 因此,要么从文件中删除每小时行,要么添加对iFile>>sTemp的调用以读取该行 此外,参数string sTemp、double dTemp和int iTemp应声明为局部变量,而不是输入参数 另外,readIn
readIn()
到达此代码时:
Alan
WakeField
IT GUY
T2034
40
15
1
Hourly
0.00
它试图读取一个double
,但是文件每小时都有Hourly
,因此读取失败
因此,要么从文件中删除每小时
行,要么添加对iFile>>sTemp的调用以读取该行
此外,参数string sTemp
、double dTemp
和int iTemp
应声明为局部变量,而不是输入参数
另外,readIn()
没有进行任何错误处理。您的main()
代码做出了一个无效的假设,即名字的向量与其他向量的大小完全匹配,但是readIn()
不能保证这一点
最后,在阅读任何内容之前检查eof()
是错误的。流的eofbit
标志在读取操作尝试读取EOF之前不会更新
你应该考虑重新编写这个代码。例如,尝试类似以下内容:
iFile >> dTemp;
sPay.push_back(dTemp);
我去了一会儿。我忘了在去发工资之前阅读小时或工薪表。我建议你阅读我也建议你了解结构和类别,以便将密切相关的数据分组。当然还有局部变量!扪心自问:如果不读取sPay
使程序工作,那么它的输入肯定有问题。在它之前你读了什么,输入文件中的下一位数据是什么?听起来你可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:哦,哇,谢谢你,我是盲人。我也会阅读其他的建议,但现在这就解决了它。
iFile >> dTemp;
sPay.push_back(dTemp);
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
struct Employee
{
std::string fName;
std::string lName;
std::string title;
std::string eID;
double hoursWorked;
double wage;
int deductions;
std::string wageType;
double sPay;
Employee() :
hoursWorked(0), wage(0), deductions(0), sPay(0)
{
}
};
void check(std::ifstream &iFile)
{
if (!iFile.is_open())
{
std::cout << "Data file not found or unable to open!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
}
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
iFile >> emp.fName;
iFile >> emp.lName;
std::getline(iFile, emp.title);
iFile >> emp.eID;
iFile >> emp.hoursWorked;
iFile >> emp.wage;
iFile >> emp.deductions;
iFile >> emp.wageType;
iFile >> emp.sPay;
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}
int main()
{
std::ifstream iFile("data.txt");
check(iFile);
std::vector<Employee> employees;
readIn(iFile, employees);
int sizeOf = employees.size();
for (int a = 0; a < sizeOf; a++)
{
std::cout << "FName " << employees[a].fName
<< " LName " << employees[a].lName
<< " JobTitle " << employees[a].title
<< std::endl;
std::cout << "EmployeeID " << employees[a].eID
<< " Hours Worked " << employees[a].hoursWorked
<< " << employees[a].wageType << " Wage " << employees[a].wage
<< std::endl;
std::cout << "Deductions " << employees[a].deductions
<< " Salary Pay " << employees[a].sPay
<< std::endl;
std::cout << std::endl;
}
std::system("pause");
return 0;
}
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::string sTemp;
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
std::getline(iFile, emp.fName);
std::getline(iFile, emp.lName);
std::getline(iFile, emp.title);
std::getline(iFile, emp.eID);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.hoursWorked))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.wage))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.deductions))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, emp.wageType);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.sPay))
iFile.setstate(std::ifstream::failbit);
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program if the file is corrupted.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}