Reading text data into array of class objects

This program has many parts but before I can move onto the next step, I have to figure out how to read data from a text file into an array of class objects. Any help will be greatly appreciated. Right now my error is no matching function or call to getline.

Text File
5 Christine Kim # 30.00 3 1
15 Ray Allrich # 10.25 0 0
16 Adrian Bailey # 12.50 0 0
17 Juan Gonzales # 30.00 1 1
18 J. P. Morgan # 8.95 0 0
22 Cindy Burke # 15.00 1 0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
    private:
        int id;             //  Employee ID
        string name;        //  Employee name
        double hourlyPay;   //  Pay per hour
        int numDeps;        //  Number of dependents
        int type;           //  Employee type

    public:
        Employee (int initId = 0, string initName = "", double initHourlyPay = 0.0, int initNumDeps = 0, int initType = 0);       //  Constructor

        bool set(int newId, string newName, double newHourlyPay, int newNumDeps, int newType);
        int getId();
        string getName();
        double getHourlyPay();
        int getNumDeps();
        int getType();

};

struct PayInfo
{
    float hours,        //  Total hours worked
          otHrs,        //  Overtime hours
          tax,          //  Tax amount deducted from gross pay
          gross,        //  Total pay before tax
          net;          //  Total pay after tax
};

Employee::Employee(int initId, string initName, double initHourlyPay, int initNumDeps, int initType)
{
    bool status = set (initId, initName, initHourlyPay, initNumDeps, initType);

    if (!status)
    {
        id = 0;
        name = "";
        hourlyPay = 0.0;
        numDeps = 0;
        type = 0;
    }
}

bool Employee::set( int newId, string newName, double newHourlyPay, int newNumDeps, int newType )
{
    bool status = false;

    if (newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && newType >= 0 && newType <= 1)
    {
        status = true;
        id = newId;
        name = newName;
        hourlyPay = newHourlyPay;
        numDeps = newNumDeps;
        type = newType;
    }

    return status;
}

int Employee::getId()
{
    return id;
}

string Employee::getName()
{
    return name;
}

double Employee::getHourlyPay()
{
    return hourlyPay;
}

int Employee::getNumDeps()
{
    return numDeps;
}

int Employee::getType()
{
    return type;
}

int main()
{
    const int num = 6;          //  Sets number of employees
    Employee *emp[num];               //  Class object
    ifstream myfile;            //  Read data from file

    //  Open data file
    myfile.open("master9.txt");
    if (!myfile)
    {
        cout <<  "Error opening data file" << endl;
    }
    else
    {
        //  Input member data
        for (int i = 0; i < num; i++)
        {
            myfile >> emp[i] -> getId();
            myfile.ignore();
            getline(myfile, emp[i] -> getName(), '#');
            myfile >> emp[i] -> getHourlyPay();
            myfile >> emp[i] -> getNumDeps();
            myfile >> emp[i] -> getType();
            myfile.ignore();
        }
        myfile.close();
    }

//    //  This is to test files are being read
//    for (int j = 0; j < num; j++)
//    {
//        cout << emp[j].id << emp[j].name << emp[j].hourlyPay << emp[j].numDeps << emp[j].type << endl;
//    }

    return 0;
}
Last edited on
You cannot assign anything to the values of the get functions. I suggest that you create another member function that takes a reference to the stream as a paramter and move the things within the for loop (on line 109) within the member function.
The loop on line 109 remains but calls the member function with myfile.
I'm really new at this and don't have much to go by except the textbook which is not that great. I have created a class function to read the files like you suggested. When I executed the file ... I am getting error reading data file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
    private:
        int id;             //  Employee ID
        string name;        //  Employee name
        double hourlyPay;   //  Pay per hour
        int numDeps;        //  Number of dependents
        int type;           //  Employee type

    public:
        Employee (int initId = 0, string initName = "", double initHourlyPay = 0.0, int initNumDeps = 0, int initType = 0);       //  Constructor

        void readContents(fstream &myfile);
        bool set(int newId, string newName, double newHourlyPay, int newNumDeps, int newType);

        //  Employee class set functions
        int getId()
        {   return id;           }

        string getName()
        {   return name;         }

        double getHourlyPay()
        {   return hourlyPay;    }

        int getNumDeps()
        {   return numDeps;      }

        int getType()
        {   return type;         }

};

struct PayInfo
{
    float hours,        //  Total hours worked
          otHrs,        //  Overtime hours
          tax,          //  Tax amount deducted from gross pay
          gross,        //  Total pay before tax
          net;          //  Total pay after tax
};

//  Employee class constructor
Employee::Employee(int initId, string initName, double initHourlyPay, int initNumDeps, int initType)
{
    bool status = set (initId, initName, initHourlyPay, initNumDeps, initType);

    if (!status)
    {
        id = 0;
        name = "";
        hourlyPay = 0.0;
        numDeps = 0;
        type = 0;
    }
}

//  Employee class set function
bool Employee::set( int newId, string newName, double newHourlyPay, int newNumDeps, int newType )
{
    bool status = false;

    if (newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && newType >= 0 && newType <= 1)
    {
        status = true;
        id = newId;
        name = newName;
        hourlyPay = newHourlyPay;
        numDeps = newNumDeps;
        type = newType;
    }

    return status;
}

void Employee::readContents(fstream &myfile)
{
    myfile >> id;
    getline(myfile, name, '#');
    myfile >> hourlyPay >> numDeps >> type;

    //  Test if data is read
    while (!myfile.fail())
    {
        cout << id << name << hourlyPay << numDeps << type;
    }
}

int main()
{
    const int NUM = 6;              //  Set number of employees
    Employee emp[NUM];              //  Array of class Employee
    ifstream myfile;                //  Read data from file
    float totalGross = 0.00,        //  Holds gross pay
          totalNet = 0.00;          //  Holds net pay
    const float taxRate = 0.15;     //  Constant tax rate



    //  Open data file
    myfile.open("master9.txt");
    if (!myfile)
    {
        cout <<  "Error opening data file" << endl;
    }

    for (int i = 0; i < NUM; i++)
    {
        Employee readContents(myfile);
    }

    myfile.close();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.