Home How to store lines from a file as a variable
Reply: 0

How to store lines from a file as a variable

user2451
1#
user2451 Published in July 16, 2018, 8:13 pm

I'd like to show the employee number name, occupation, and department of employees from a text file called organisation.txt, and save them in the variables declared in the class OrganisationRecord.

How can I do that?

#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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30696 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO