Trouble with structures

Hello everyone I was having some trouble with my code in which I keep getting errors (in bold) that say error: no match for 'operator[]' in 'employee[i]'. I've looked on the forums and have found some solved problems yet somehow I can get mine to work. I just wanted to know how I can fix these issues. Thanks.

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int NAMELEN = 65;
const int SIZE = 20;

struct Payroll
{
    char name;
    double rate;
    double hours;
};

void printMenu();
void addEmployees(Payroll, int &count);
void displayAllEmployees(Payroll, int count);
void printEmployeePaycheck(Payroll, int count);
void printAllPaychecks(Payroll, int count);

int main()
{
    int count=0, choice;
    Payroll employee;

    do
    {
        printMenu();
        cin >> choice;
        cout << endl;

        switch(choice)
        {
            case 1:
                addEmployees(employee, count);
                break;
            case 2:
                displayAllEmployees(employee, count);
                break;
            case 3:
                printEmployeePaycheck(employee, count);
                break;
            case 4:
                printAllPaychecks(employee, count);
                break;
            case 0:
                cout << "Goodbye. " << endl << endl;
                break;
            default:
                cout << "Invalid menu choice. Please enter valid ";
                cout << "menu choice " << endl;
                break;
        }
    }
    while(choice!=0);
    return(0);
}

void printMenu()
{
    cout << endl;
    cout << "Welcome to the Employee Paycheck Calculator " << endl;
    cout << "=========================================== " << endl;
    cout << "1.  Add employee record(s) to the database " << endl;
    cout << "2.  Print all employee records " << endl;
    cout << "3.  Print the paycheck for one employee " << endl;
    cout << "4.  Print paychecks for all employees " << endl << endl;
    cout << "0.  Exit " << endl;
    cout << "=========================================== " << endl;
    cout << "Enter selection:  ";
}

void addEmployees(Payroll employee, int &count)
{
    cout << "How many employees do you wish to add? ";
    cin >> count;

    
    while (count < 1 || count > 20)
    {
        cout << "That is an invalid size. ";
        cout << "You must enter a value between 1 and 20. " << endl;
        cout << "How many employees do you wish to add? ";
        cin >> count;
    }

    for (int i = 0; i < count; i++)
    {
        cout << endl;
        cout << "Enter Employee " << i << " name: ";
        cin >> employee[i].name;

        cout << "Enter " << employee[i].name << "'s pay rate: ";
        cin >> employee[i].rate;

        cout << "Enter " << employee[i].name << "'s weekly hours: ";
        cin >> employee[i].hours;
    }
}

void displayAllEmployees(Payroll employee, int count)
{
    if (count == 0)
    {
        cout << "You must add atleast one employee to the database! " << endl;
        cout << "Please select option 1 to add employees to database. " << endl;
        return;
    }

    else
    {
        cout << endl;
        cout << setw(8) << left << "Index" << setw(20) << left << "Name";
        cout << setw(16) << left << "Rate" << setw(5) << left << "Hours";
        cout << endl;

        cout << fixed << showpoint << setprecision(2);
 for (int i = 0; i < count; i++)
        {
            cout << setw(8) << left << i << setw(20) << left;
            cout << employee[i].name << setw(16) << left << employee[i].rate;
            cout << setw(5) << left << employee[i].hours << endl;
        }
    }
}

void printEmployeePaycheck(Payroll employee, int count)
{
    int i = -1;

    if (count == 0)
    {
        cout << "You must add atleast one employee to the database! " << endl;
        cout << "Please select option 1 to add employees to database. " << endl;
        return;
    }

    cout << "Enter an Employee Number: ";
    cin >> i;
    cout << endl;

    while (i < 0 || i > count-1)
    {
        cout << "Error: this number does not correspond to an employee. " << endl;
        cout << "Please enter a valid number. " << endl;
        cin >> i;
    }

    cout << "Employee: " << setw(16) << left << employee[i].name;
    cout << "Rate: " << setw(8) << left << employee[i].rate << "Hours: ";
    cout << setw(8) << left << employee[i].hours << "Weekly Pay: $";
    cout << setw(5) << left << employee[i].rate * employee[i].hours << endl;
}

void printAllPaychecks(Payroll employee, int count)
{
 if (count == 0)
    {
        cout << "You must add atleast one employee to the database! " << endl;
        cout << "Please select option 1 to add employees to database. " << endl;
        return;
    }

    else
    {
        cout << fixed << showpoint << setprecision(2);

        for (int i = 0; i < count; i++)
        {
            cout << "Employee: " << setw(16) << left << employee[i].name;
            cout << "Rate: " << setw(8) << left << employee[i].rate[i];
            cout << "Hours: " << setw(8) << left << employee[i].hours;
            cout << "Weekly Pay: $" << setw(5) << left;
            cout << employee[i].rate * employee[i].hours << endl;
        }
    }
}


Last edited on
closed account (D80DSL3A)
The [] operator is usually used with arrays, but your code isn't using any arrays.
It looks like you should be using arrays for several things though.
Assuming you wrote the code you've posted, why did you write lines 6 and 7?
1
2
const int NAMELEN = 65;
const int SIZE = 20;

What is the planned usage for these constants?
actually yes I'm using arrays, it is supposed to be done as an array of structures. I wrote my code based on examples from a textbook though obviously it's not written correctly. The two constants are supposed to represent the maximum characters allowed for a name (NAMELEN) and the maximum amount of employees I can add (SIZE). I assume then I should change the struct to;

1
2
3
4
5
6
 struct Payroll
{
       char name[];
       double rate [];
       double hours[];
}


and add the [] at the end of each "employee" in my function calls?
closed account (D80DSL3A)
I think only name would be an array in the structure:
1
2
3
4
5
6
struct Payroll
{
       char name[NAMELEN];
       double rate;
       double hours;
}

Then declare an array of Payoll objects in main():
1
2
//Payroll employee;// just one?
Payroll employee[SIZE];// array of 20 

Place the [] after Payroll in each function header. Example:
void printAllPaychecks(Payroll[] employee, int count)
It compiled and worked great! Thanks for all your help.
Topic archived. No new replies allowed.