Why is my array of structures not working

I just made this program and i'm not understanding why all of my errors are only for when the structure variables are trying to be used, so if anyone could help me figure out what's wrong here then i'd really appreciate it.

request for member `name' in `emp', which is of non-class type `employee[((unsigned int)((int)i))]'


All of the errors say that except change which part of the structure it's talking about.

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


struct employee
{
       string name;
       double hours;
       double rate;
       int age;
       double basewage;
       double tax;
};

int main()
{

    ifstream infile;
    infile.open("Homework 2 datafile.txt");
    int i, k, overtime, maxage = 0, minage = 0;
    double overwage, maxtax = 0;
    string option, taxname;
    cout << "Miser Corporation Payroll" << endl;
    
    cout << "Will data be inputted through a file? (respond with 'yes' or 'no')" << endl;
    cin >> option;
    cout << "How many employees are in the company?" << endl;
    cin >> i;
    employee emp[i];
    if (option == "no"){
               for (k = 0; k < i; k++){
                                  cout << "Enter Employee Name" << endl;
                                  cin >> emp.name[k];
                                  cout << "Enter Hours Worked" << endl;
                                  cin >> emp.hours[k];
                                  cout << "Enter Rate of Pay" << endl;
                                  cin >> emp.rate[k];
                                  cout << "Enter age" << endl;
                                  cin >> emp.age[k];}}
    else{
         for (k = 0; k < i; k++){
             infile >> emp.name[k];
             infile >> emp.hours[k];
             infile >> emp.rate[k];
             infile >> emp.age[k];
             if (minage == 0)
                minage = emp.age[k];
             else if (minage > emp.age[k])
                  minage = emp.age[k];
             if (maxage < emp.age[k])
                maxage = emp.age[k];}}
    
    for (k = 0; k < i; k++){
        if (emp.hours[k] > 40){
           overtime = emp.hours[k] - 40;
           emp.wage[k] = 40 * emp.rate[k];
           overwage = overtime * (emp.rate[k] * 1.5);
           emp.wage[k] += overwage;}
        else
            emp.wage[k] = emp.hours[k] * emp.wage[k];
        cout >> emp.name[k] >> endl >> emp.hours[k] >> "   " >> emp.rate[k] >> "   " >> emp.age[k] >> endl;}
            
    for (k = 0; k < i; k++){
        if (emp.age[k] > 55)
           emp.tax[k] = emp.wage[k] * .5;
        else
            emp.tax[k] = emp.wage[k] * .1;
        if (maxtax < emp.tax[k]){
           maxtax = emp.tax[k];
           taxname = emp.name[k];}}
            
                          
}
Oh uh, thanks.

don't really understand how to fix it though
i still can't get the coding for the array right, i'm reading and rereading it but i do not understand this.
what is the coding supposed to be in order to get this to work? i've obviously never done this before
closed account (D80DSL3A)
Your notation is wrong where you try to access members of your emp array.
Incorrect: emp.xyz[k]
Correct: emp[k].xyz
It looks like there are 30-40 places in your code where this correction needs to be made.

I think that kbw was referring to your illegal use of an array with a dimension determined at run time:
1
2
3
cout << "How many employees are in the company?" << endl;
    cin >> i;
    employee emp[i];// no no no! i must be a pre-determined constant. 

However, some compilers apparently allow this. Maybe it will work on your system.
If you wish to do it correctly look into either the STL vector object or dynamic memory allocation.
well, i got it working (thanks), but for some reason i'm not getting what i should be getting as the input of my file. It's saved in the right place, emp.name should come out as a word but it's coming out as some random sequence of numbers, any idea what could be causing this?
Topic archived. No new replies allowed.