简体   繁体   中英

how can i push back an in into a vector with different data type

I have two classes, one called " OrganisationRecord " and the other called " PayrollProcessing ". " OrganisationRecord " contains the name occupation and department of an employee.

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

class PayrollProcessing
{
private:
    ifstream inputfile;
    ofstream outputfile;
    vector<OrganisationRecord> OrganisationRecords;
public:
    void loadOrganisationRecords(string filename);
    void displayEmployeeOfSalaryGTE(double salary);

    //GTE = greater than or equal to
};

within " PayrollProcessing " are two member functions; " loadOrganisationRecords " and " displayEmployeeOfSalaryGTE " the code for which are:

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)
        {
            inputfile.ignore();
            getline(inputfile, _organisationrecord.name);
            getline(inputfile, _organisationrecord.occupation);
            getline(inputfile, _organisationrecord.department);

            OrganisationRecords.push_back(_organisationrecord);
        }

        inputfile.close();
}

void PayrollProcessing::displayEmployeeOfSalaryGTE(double salary)
    {
        unsigned int count;
        salary = SALARY;

        if (salary < 0)
        {
            cout << "no record match this criteria!" << endl;
            return;
        }
        for (count = 0; count < PayrollRecords.size(); count++)
        {
            if (PayrollRecords[count].salary >= salary)
            {
                cout << "=============================================" << endl;
                cout << "Employeenumber: " << endl << endl;
                cout << "Name: " << OrganisationRecords[count].name << endl;
                cout << "Adress: " <<  HRRecords[count].address << endl;
                cout << "Department: " << OrganisationRecords[count].department << endl;
                cout << "Salary: " << PayrollRecords[count].salary << endl;
                cout << "=============================================" << endl;
                cout << OrganisationRecords.size();
            }
        }

        return;
    }

how can i implicitly make the employee number available for display in " displayEmployeeOfSalaryGTE ".

According to your reading code, every organization record has an employee number. So simply add a field for that value to your OrganisationRecord class, then you will have access to it in displayEmployeeOfSalaryGTE() , eg:

class OrganisationRecord
{
public:
    int employeeNumber; // <-- add this
    ...
};

...

void PayrollProcessing::loadOrganisationRecords(string filename)
{
    ...

    int employeeNumber;
    OrganisationRecord _organisationrecord;

    while (inputfile >> employeeNumber)
    {
        inputfile.ignore();
        _organisationrecord.employeeNumber = employeeNumber // <-- add this
        ...
    }

    ...
}

...

cout << "EmployeeNumber: " << OrganisationRecords[count].employeeNumber << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM