2017-12-06 2 views
-1

organisation.txt이라는 텍스트 파일에서 직원 번호 (클래스를 선언하지 않고), 이름, 직업 및 직원을 표시하고 싶습니다. 변수는 OrganisationRecord 클래스에 선언되었습니다.파일에서 데이터 읽기 및 변수에 저장

어떻게 텍스트 파일의 데이터를 가져 와서 해당 변수에 저장할 수 있습니까?

#include <iostream> 
#include <string> 
#include <vector> 
#include <fstream> 

#define ORGANISATIONALRECORDSFILE "organisation.txt" 
#define HRRECORDSFILE "HR_records.txt" 
#define PAYROLLRECORDSFILE "payroll_records.txt" 

using namespace std; 


class OrganisationRecord 
{ 
private: 
public: 
    string name; 
    string occupation; 
    string department; 
}; 

class HRRecord 
{ 
private: 
public: 
    string address; 
    string phonenumber; 
    string ninumber; 
}; 

class PayrollRecord 
{ 
private: 
public: 
    string ninumber; 
    double salary; 
}; 

class PayrollProcessing 
{ 
private: 
    ifstream inputfile; 
    ofstream outputfile; 
    vector<OrganisationRecord> OrganisationRecords; 
    vector<HRRecord> HRRecords; 
    vector<PayrollRecord> PayrollRecords; 
public: 
    void loadOrganisationRecords(string filename); 
    void loadHRRecords(string filename); 
    void loadPayrollRecords(string filename); 
    void displayEmployeeOfSalaryGTE(double salary); 
    //GTE = greater than or equal to 
}; 

void PayrollProcessing::loadOrganisationRecords(string filename) 
{ 
    inputfile.open(ORGANISATIONALRECORDSFILE); 

    if (!inputfile) 
    { 
     cout << "the organisation records file does not exist" << endl; 
     return; 
    } 

     OrganisationRecord _organisationrecord; 
     int employeenumber; 


     while (inputfile >> employeenumber) 
     { 
      while (inputfile >> _organisationrecord.name) 
      { 
       cout << _organisationrecord.name; 
       cout << _organisationrecord.occupation; 
       cout << _organisationrecord.department <<endl; 
      } 

      OrganisationRecords.push_back(_organisationrecord); 
     } 

} 



int main(void) 
{ 
    PayrollProcessing database1; 
    database1.loadOrganisationRecords(ORGANISATIONALRECORDSFILE); 

    return 0; 
} 

organisation.txt 여기

0001 
Stephen Jones 
Sales Clerk 
Sales 
0002 
John Smith 
Programmer 
OS Development 
0003 
Fred Blogs 
Project Manager 
Outsourcing 
+0

는'?'나는 당신이 오류가 나는의 명백한 문제 외에도 질문 – UKMonkey

+0

는 "왜 당신이 원하는 것이 정확히 어떤 상태로 질문을 바꿔 필요가 있다고 생각 "그 다음 질문은"수업에서 선언하지 않고 정확히 무엇을 의미합니까? " 그 유형의 레코드에 대한 유효한 데이터 조각이라면 실제로 그 속성/필드가 설정되어 있어야합니다 ... 또한 명명 규칙, 클래스의 이름 지정 방법 등을 다시 생각해 볼 수도 있습니다. 당신은 회사를 설명하고 있지만 분야에 따라 사실 직원 정보가 나와 있습니다 –

+0

을 편집 한 확인 – Taegost

답변

0

어떤 오류가 입력을 확인하지 않고 작동 접근법이다. 다른 사람들이 말한 것처럼 std::getline()을 사용하여 줄 단위로 파일을 읽어야합니다. 변수에 직접 입력하면 입력 된 구문 분석이 전체 줄과 달리 텍스트의 공백으로 구분되기 때문입니다. 내가 그렇게 할 수있는 방법

OrganisationRecord _organisationrecord; 
std::string employeeNumberStr; 
int employeenumber; 

// without any robust error checking... 
while (std::getline(inputfile, employeeNumberStr)) 
{ 
    // store employee number string as an integer 
    std::stringstream ss; 
    ss << employeeNumberStr; 
    ss >> employeenumber; 

    std::getline(inputfile, _organisationrecord.name); 
    std::getline(inputfile, _organisationrecord.occupation); 
    std::getline(inputfile, _organisationrecord.department); 

    // debug print results 
    std::cout << "id number: " << employeenumber << std::endl; 
    std::cout << "name: " << _organisationrecord.name << std::endl; 
    std::cout << "occupation: " << _organisationrecord.occupation << std::endl; 
    std::cout << "department: " << _organisationrecord.department << std::endl; 

    OrganisationRecords.push_back(_organisationrecord); 
} 
+0

감사합니다 저스틴 !! 나는 지금 내가 잘못 가고있는 곳을 본다. 나를 미치게 만들었다. –