2017-12-06 17 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

を編集した[OK]を – 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

ありがとう、ジャスティン!!私は今どこが間違っているのか見ています。それは私を夢中にさせていた –

関連する問題