2014-08-28 67 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; 
}; 

缺少的格式化輸入和輸出作爲練習留給讀者。

相關問題