Storing Class members inside an array

I'm trying to store class members inside an array. I can't use vectors yet. So how would I do it? In terms of syntax classes look similar to structs so I tried storing the class members like how I would a struct but I got an error of having too many initializers. So how would I do it? thank you!

Objects of class and struct type are treated identically in this respect.

If you have questions regarding code, please supply the code you have a question about.
Here's my code. When I try running it I get an error saying that there are too many initializers.
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
#include<iostream>
#include<iomanip>
using namepsace std;
int main()
{
class employeerecord
{
    string employeeid;
    string lastname;
    string firstname;
    float hours;
    float payrate; // per hour
    float grosspay;
};

employeerecords employee[maximumrecords] = {"De Bosse", "Ian", 34.5, 45.75, 0.05, "Smithers", "Weylan K.S.", 40.1, 28.75, 0.04,"Simpson", "Homer J.", 40, 23.44, 0.023};
cout << "Display All Employees" << endl;
                ptToArray = &employee[0];


                for(int i = 0; i <= maximumrecords - 1; i++ )
                {
                        cout << "Employee #" << (i + 1) << endl;
                        cout << ptToArray[i].lastname << ", " << ptToArray[i].firstname << endl;
                        cout << "Hours Worked: " <<  ptToArray[i].hoursworked << " $" << ptToArray[i].hoursworked << endl;
                        cout << "Employee Pay Rate: " << ptToArray[i].payrate << " $" << ptToArray[i].payrate << endl;
                        cout << "Employee Tax Rate: " << ptToArray[i].taxrate * 100 << "%" << endl;
                }
}
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
#include<iostream>
#include<iomanip>
using namepsace std;

class employeerecord
{
    string employeeid;
    string lastname;
    string firstname;
    float hours;
    float payrate; // per hour
    float grosspay;
};


int main()
{

    employeerecords employee[maximumrecords] = {
        {"De Bosse", "Ian", 34.5, 45.75, 0.05},
        { "Smithers", "Weylan K.S.", 40.1, 28.75, 0.04},
        {"Simpson", "Homer J.", 40, 23.44, 0.023}
    };

    cout << "Display All Employees" << endl;
    ptToArray = &employee[0];


    for(int i = 0; i <= maximumrecords - 1; i++ )
    {
         cout << "Employee #" << (i + 1) << endl;
         cout << ptToArray[i].lastname << ", " << ptToArray[i].firstname << endl;
         cout << "Hours Worked: " <<  ptToArray[i].hoursworked << " $" << ptToArray[i].hoursworked << endl;
         cout << "Employee Pay Rate: " << ptToArray[i].payrate << " $" << ptToArray[i].payrate << endl;
         cout << "Employee Tax Rate: " << ptToArray[i].taxrate * 100 << "%" << endl;
     }
}
Last edited on
Thank you! I see what you did.
Actually, when I try to add a constructor to it, I get an error that it couldn't convert the contents of the array and extended initialization only available with -std=c++11

this is what I added to the class
employeerecords()
{
lastname = "N/A";
firstname = "N/A";
payrate = 0;
hoursworked = 0;
taxrate = 0;
}
Topic archived. No new replies allowed.