2017-12-05 16 views
-2

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

[OK]をクリックすると、コードが表示され、何をしたいのですか。質問は何ですか? –

+0

ファイルからデータを読み込み、適切な変数に格納するにはどうすればよいですか? –

答えて

0

あなたがinputfile >> _organisationrecord.nameを使用するとき、それは最初の空白文字で停止します。したがって、Stephenのみが読み取られ、organisationrecord.nameに格納されます。

戦略を少し変更する必要があります。

  1. ファイルの内容を1行ずつ読みます。行がなくなったら停止します。
  2. それぞれの行を適切に処理します。

ここで入力を処理する方法の1つです。

std::string line; 
while (std::getline(inputfile, line)) 
{ 
    // Extract the employeenumber from the line 
    std::istringstream str(line); 
    if (!(str >> employeenumber)) 
    { 
     // Problem reading the employeenumber. 
     // Stop reading. 
     break; 
    } 

    if (!std::getline(inputfile, line)) 
    { 
     // Problem reading the next line. 
     // Stop reading. 
     break; 
    } 

    _organisationrecord.name = line; 

    if (!std::getline(inputfile, line)) 
    { 
     // Problem reading the next line. 
     // Stop reading. 
     break; 
    } 
    _organisationrecord.occupation = line; 

    if (!std::getline(inputfile, line)) 
    { 
     // Problem reading the next line. 
     // Stop reading. 
     break; 
    } 
    _organisationrecord.department = line; 

    std::cout << _organisationrecord.employeenumber << std::endl; 
    std::cout << _organisationrecord.name << std::endl; 
    std::cout << _organisationrecord.occupation << std::endl; 
    std::cout << _organisationrecord.department << endl; 

    OrganisationRecords.push_back(_organisationrecord); 
} 
+0

ありがとう、ありがとう、ありがとう!これで一日中過ごしました!! –

関連する問題