我的構建在這裏完全成功,但沒有輸出到我的文本文件,我知道我幾天前問了一個關於這個程序的問題,而且我已經改變了它。我現在做錯了什麼?在這裏得到一個錯誤,但不知道它是什麼
在此先感謝你們。 我正在嘗試從employeesIn.txt文件輸入,並創建員工結構構成的employeeOut.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
輸出應該看起來像
員工記錄:123 名稱:約翰·布朗 家庭住址:125普拉里街 士丹頓,IL 62088
員工記錄:124 名稱馬特·拉森 家庭地址:....等等
這是我的代碼。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct Person {
string first;
string last;
};
struct Address {
string street;
string city;
string state;
string zipcode;
};
struct Employee {
Person name;
Address homeAddress;
int eid;
};
int readEmployee(istream& in, Employee eArray[]);
void displayEmployee(ostream& out,Employee eArray[], int EmployeePopulation);
const int arr=50;
Employee eArray[arr];
ifstream fin;
ofstream fout;
int main(int argc, const char * argv[])
{
fin.open("employeesIn.txt");
if (!fin.is_open()) {
cerr << "Error opening employeesIn.txt for reading." << endl;
exit(1);
}
fout.open("employeesOut.txt");
if (!fout.is_open()) {
cerr << "Error opening employeesOut.txt for writing." << endl;
exit(1);
}
int tingle = readEmployee(fin, eArray);
fin.close();
displayEmployee(fout, eArray, tingle);
fout.close();
exit(0);
}
int readEmployee(istream& in, Employee eArray[])
{
string eidText;
string line;
getline(in, line);
int EmployeePopulation = 0;
while (!in.eof()) {
getline(in, eidText, ',');
eArray[EmployeePopulation].eid = stoi(eidText);
getline(in, eArray[EmployeePopulation].name.first, ',');
getline(in, eArray[EmployeePopulation].name.last, ',');
getline(in, eArray[EmployeePopulation].homeAddress.street, ',');
getline(in, eArray[EmployeePopulation].homeAddress.city, ',');
getline(in, eArray[EmployeePopulation].homeAddress.state, ',');
getline(in, eArray[EmployeePopulation].homeAddress.zipcode);
EmployeePopulation++;
}
return EmployeePopulation;
}
void displayEmployee(ostream& out, Employee eArray[], int EmployeePopulation)
{
for (int i = 0; i <= EmployeePopulation - 1; i++) {
out << "Employee Record: " << eArray[i].eid
<< endl
<< "Name: " << eArray[i].name.first << " " << eArray[i].name.last
<< endl
<< "Home address: " << eArray[i].homeAddress.street
<< endl
<< eArray[i].homeAddress.city << ", " << eArray[i].homeAddress.state << " " << eArray[i].homeAddress.zipcode
<< endl
<< endl;
}
}
什麼是確切的錯誤? – 2014-08-28 00:47:31
我沒有收到錯誤,它只是不會輸出到文件中,因爲我認爲它會。 – MatthewTingle 2014-08-28 00:50:10
那麼你的標題相當具有誤導性。你有沒有在調試器中完成程序?你確定有什麼被讀嗎? 'while(!fin.eof())'幾乎總是錯誤的,你應該從實際讀取中檢查返回值,不希望在發生問題後發現問題。 – 2014-08-28 00:52:12