我以为我完全解决了这个问题,但实际上并没有在文件中写任何东西。它打开了employeesOut.txt文件但没有写任何东西。任何猜测?我收到了错误
以下是请求的输入文件。
123,John,Brown,125 Prarie Street,Staunton,IL,62088
124,Matt,Larson,126 Hudson Road,Edwardsville,IL,62025
125,Joe,Baratta,1542 Elizabeth Road,Highland,IL,62088
126,Kristin,Killebrew,123 Prewitt Drive,Alton,IL,62026
127,Tyrone,Meyer,street,999 Orchard Lane,Livingston,62088
libc ++ abi.dylib:以std :: invalid_argument类型的未捕获异常终止:stoi:无转换 (lldb)
我相信错误在我的main.cpp中,所以就是这样。
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
using namespace std;
bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);
int readFromFile(ifstream& in, Employee empArray[]);
void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);
int main(){
ifstream fin;
ofstream fout;
if(!openFileForReading(fin, "employeesIn.txt")) {
cerr << "Error opening employeesIn.txt for reading." << endl;
exit(1);
}
if(!openFileForWriting(fout, "employeesOut.txt")) {
cerr << "Error opeing employeesOut.txt for writing." << endl;
exit(1);
}
Employee employeeArray[50];
int employeeCount = readFromFile(fin, employeeArray);
fin.close();
writeToFile(fout, employeeArray, employeeCount);
fout.close();
cout << "Program successful." << endl << endl;
return 0;
}
bool openFileForReading(ifstream& fin, const string& filename) {
fin.open("employeesIn.txt");
return (fin.is_open());
}
bool openFileForWriting(ofstream& fout, const string& filename) {
fout.open("employeesOut.txt");
return (fout.is_open());
}
int readFromFile(ifstream& in, Employee empArray[]) {
int temp = 0;
string eidText;
string first;
string last;
string street;
string city;
string state;
string zipcode;
while(!in.eof()) {
getline(in, eidText, ',');
getline(in, first, ',');
getline(in, last, ',');
getline(in, street, ',');
getline(in, city, ',');
getline(in, state, ',');
getline(in, zipcode, ',');
empArray[temp].setEid(stoi(eidText));
empArray[temp].setName(first, last);
empArray[temp].setAddress(street, city, state, zipcode);
temp++;
}
return temp;
}
void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {
for (int i = 0; i < numberOfEmployees; i++){
out << "Employee Record: " << empArray[i].getEid()
<< endl
<< "Name: " << empArray[i].getName()
<< endl
<< "Home Address: " << empArray[i].getAddress()
<< endl
<< endl;
}
}
答案 0 :(得分:2)
您的初始问题已通过将getline(in, zipcode, ',');
行更改为getline(in, zipcode, '\n');
来解决,因为这会使getline
以换行字符结束。当stoi
被赋予“马特”作为参数时,会产生错误。
但是,您的readFromFile
函数正在接收数组的副本,因此它所做的更改不会移回main()
函数(因为您返回的是计数,而不是数组)。
由于传递引用数组很乱,请尝试使用std :: vector(在#include
<vector>
之后)对您的员工进行分组。如果Employee.H
指定了无参数构造函数或没有构造函数(并且您的编译器中有默认构造函数),那么
std::vector<Employee> employeeVector;
void readFromFile(ifstream& in, std::vector<Employee> &empVec);
void readFromFile(ifstream& in, std::vector<Employee> &empVec) {
// int temp = 0; unneeded.
string eidText;
...
string zipcode;
while(!in.eof()) {
getline(in, eidText, ',');
...
getline(in, zipcode, '\n');
Employee tempEmp();
tempEmp.setEid(stoi(eidText));
...
empVec.push_back(tempEmp);
// temp++; no longer need this
}
}
可能是更好的选择。即使构造函数非常复杂,它仍然可能比引用数组更简单(你必须更改Employee tempEmp();
行。顺便说一下,如果这个抱怨并且不会编译,我可能会遇到MVP错误 - 删除()s你会没事的。
修改强>:
由于您已被指示使用Employee
s数组,因此您可以选择返回数组,如果您愿意,或者通过指针传递它应该是合法的。这将要求您以与原始代码类似的方式沿着数组移动指针进行迭代。我已经离开vector
方法,因为我认为它更干净,但由于你不能使用它,你可以选择两种选择(返回数组,传递指针而不是对象)。我的猜测是你应该选择指针,因为这现在似乎是一个家庭作业式的问题。
在这种情况下,您可以使用
int main() {
...
Employee* empPtr = employeeArray;
...
}
void readFromFile(ifstream& in, Employee* empPtr) {
string eidText;
...
string zipcode;
while(!in.eof()) {
getline(in, eidText, ',');
...
getline(in, zipcode, '\n');
(*empPtr).setEid(stoi(eidText));
...
empPtr++;
}
}