David Tobin
David Tobin

Reputation: 19

Class function to take data from a file and construct from another class to save it in a Vector C++

I am a first year in Computer Science learning C++. We have an assignment where we have to load Data from a file, use a constructor and save this data in a Vector. I have scoured our Lecture Notes and there is no examples of how to do this and I have googled it for hours now to find nothing. Maybe I am asking the wrong thing?

I have left all the other classes and functions out for now as I have not worked on them yet.

Here is the code that I managed so far:

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<vector>

using namespace std;

class OrganisationRecord
{
private:

public:
    string name;
    string occupation;
    string department;
};
class payrollprocessing
{

private:
    ifstream inputfile;
    ofstream outputfile;
    vector<OrganisationRecord> OrganisationRecords; //Vector it needs to be saved too
    vector<HRRecord> HRRecords;
    vector<PayRollRecord> PayRollRecords;

public:
    void loadOrganisationRecords(string filename); //Function I am trying to write
    void loadHRRecords(string filename);
    void loadPayrollRecords(string filename);
    void displayEmployeeOfSalaryGTE(double salary);
    //GTE = Greater than or equal to
};
void payrollprocessing::loadOrganisationRecords(string name)
{
    inputfile.open("organisation.txt");

    if (!inputfile.is_open())
    {
        cout << "File Not Open Error!";
    }
    else
    {
        std::string line;
        int i = 0;
        while (!inputfile.eof())
        {
            OrganisationRecord OrgRec;             //Here is the code to 
            inputfile >> OrgRec.name;              //to construct and then
            inputfile >> OrgRec.occupation;        //save to the vector
            inputfile >> OrgRec.department;        //we havent learned how to
                                                   //how to print string 
            OrganisationRecords.push_back(OrgRec); //vectors yet which makes
            i++;                                   //it hard to test
        }

    }
    inputfile.close();

}
int main(void)
{
    string name;
    payrollprocessing PP;
    PP.loadOrganisationRecords(name);

    return 0;
}

We also cant edit the libraries or the classes. All help with this is much appreciated!

Upvotes: 0

Views: 228

Answers (2)

Dimitrie Vatra
Dimitrie Vatra

Reputation: 3

I created a constructor for your class

public:
payrollprocessing();

with no parametters, witch will read all data from your file, and write in your list. A constructor is a function withowt type, and the name of it's class;

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<vector>

using namespace std;

class OrganisationRecord
{
private:

public:
string name;
string occupation;
string department;
};
class payrollprocessing
{
private:
    ifstream inputfile;
    ofstream outputfile;
    vector<OrganisationRecord> OrganisationRecords;

public:
    payrollprocessing();
//GTE = Greater than or equal to
};
payrollprocessing::payrollprocessing()
{
    inputfile.open("organisation.txt");

    if (!inputfile.is_open())
    {
        cout << "File Not Open Error!";
    }
else
{
    std::string line;
    int i = 0;
    while (!inputfile.eof())
    {
        OrganisationRecord OrgRec;             //Here is the code to
        inputfile >> OrgRec.name;              //to construct and then
        inputfile >> OrgRec.occupation;        //save to the vector
        inputfile >> OrgRec.department;        //we havent learned how to
                                               //how to print string
        OrganisationRecords.push_back(OrgRec); //vectors yet which makes
        i++;                                   //it hard to test
    }

}
inputfile.close();
}
int main(void)
{
    string name;
    payrollprocessing PP();
    return 0;
}

So ... in main function, when you write payrollprocessing PP(); you call the constructor. The scope of constructor is to initializate the values of variables. For example, the constructor af a button draw the button on the screen, set his location and size (from parametters, but in your case we don't have one); I removed your functions, but you dont have to :)

Upvotes: 0

Andrew
Andrew

Reputation: 518

Your classes do not have constructors defined. Add a constructor to call your loadOrganisationRecords function. I suspect your searches have been too specific. Try searching for "C++ constructors" by itself and you should come up with a wealth of information.

It looks like you have the right ideas for file I/O and vector usage, but searching for those on their own too is not a bad idea either.

Upvotes: 1

Related Questions