Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
188 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you use inputfile >> _organisationrecord.name it stops at the first whitespace character. Hence, only Stephen will be read and stored in organisationrecord.name.

You need to change your strategy a bit.

  1. Read the contents of the file line by line. Stop when there are no more lines left.
  2. Process each line as you seem fit.

Here's one way to deal with the input.

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...