2014-08-28 54 views
0

我的构建在这里完全成功,但没有输出到我的文本文件,我知道我几天前问了一个关于这个程序的问题,而且我已经改变了它。我现在做错了什么?在这里得到一个错误,但不知道它是什么

在此先感谢你们。 我正在尝试从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; 
    } 
} 
+0

什么是确切的错误? – 2014-08-28 00:47:31

+0

我没有收到错误,它只是不会输出到文件中,因为我认为它会。 – MatthewTingle 2014-08-28 00:50:10

+2

那么你的标题相当具有误导性。你有没有在调试器中完成程序?你确定有什么被读吗? 'while(!fin.eof())'几乎总是错误的,你应该从实际读取中检查返回值,不希望在发生问题后发现问题。 – 2014-08-28 00:52:12

回答

1

两件事情:

你应该在主结尾使用return 0而非exit(0)

检查eof您执行了几次读取并试图转换数据后是错误的。您需要检查读取失败。

这将更正eof问题。程序崩溃了,因为stoi在读取失败时抛出异常。

int readEmployee(istream& in, Employee eArray[]) 
{ 
    string eidText; 
    string line; 
    //This discards the first line. Incorrect for the test data you supplied. 
    getline(in, line); 

    int EmployeePopulation = 0; 
    //Check for errors while reading, not eof after the fact. 
    //This was crashing because stoi failed when no data was 
    //read due to eof being true after the loop check. 
    while( getline(in, 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)) 
    { 
     eArray[EmployeePopulation].eid = stoi(eidText); 
     EmployeePopulation++; 
    } 
    return EmployeePopulation; 
} 
0

在你readEmployee功能,您通过isstream& in。我想你应该检查while (!in.eof())答不while (!fin.eof())。 而你的getline(fin, line);也应该是getline(in, line);

+0

已经对上面的代码进行了更新,输出仍然没有任何结果:/ – MatthewTingle 2014-08-28 01:07:22

1

,如果您使用的Employee一个vector你会少的问题。
您可以通过引用功能来传递它。
这些功能可以通过使用std::vector::size()来获得员工人数。 使用push_back方法时,std::vector自动扩展。

如果你创建了自己的类的输入和输出方法,你就不必破坏封装:

class Person // using class to support privacy and encapsulation 
{ 
    std::string first_name; 
    std::string last_name; 
    public: 
    friend std::istream& operator>>(std::istream& inp, Person& p); 
    friend std::ostream& operator<<(std::ostream& out, const Person& p); 
}; 

std::istream& operator>>(std::istream& inp, Person& p) 
{ 
    std::getline(inp, p.first_name, ','); 
    std::getline(inp, p.last_name, ','); 
} 

std:ostream& operator<<(std::ostream& out, const Peron& p) 
{ 
    out << "Name: "; 
    out << p.first_name; 
    out << " "; 
    out << p.last_name; 
} 

class Employee 
{ 
    Person name; 
    Address addr; 
    public: 
    friend std::istream& operator>>(std::istream& inp, Employee& e); 
}; 

std::istream& operator>>(std::istream& inp, Employee& e) 
{ 
    inp >> name; 
    inp >> addr; 
}; 

缺少的格式化输入和输出作为练习留给读者。

相关问题