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

How to store lines from a file as a variable

Ensa Touray
1#
Ensa Touray Published in 2017-12-05 23:35:12Z

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
R Sahu
2#
R Sahu Reply to 2017-12-05 23:45:07Z

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

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

© 2016 Powered by mzan.com design MATCHINFO