Gvegas222
Gvegas222

Reputation: 223

Making an array of structures?

I just made a struct that stored all the information about an employee together in one unit. Now I have to take all that information and put it in an array of structures called employees.

This is my struct:

struct EmployeeT
{
    char name[MAXSIZE];
    char title;
    double gross;
    double tax;
    double net;
};

Now how do I put that information into an array?

Thanks again guys

Upvotes: 0

Views: 224

Answers (3)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

int n;
cout<<"Enter number of records: ";
cin>>n
employeeT *ptr_e=new employeeT[n]

Upvotes: 0

Chiara Coetzee
Chiara Coetzee

Reputation: 4429

In C, you can create a fixed-size array of EmployeeT structs using this syntax:

struct EmployeeT employees[10];

The "struct EmployeeT" indicates the type of each element of the array, while the "[10]" indicates that it is an array of 10 elements. In C++, the "struct" keyword is optional and can be omitted:

EmployeeT employees[10];

You can then enter information into the array like this:

employees[2].tax = 2000.00;

This sets the tax of the 3rd employee in the array to 2000.00 (3rd because it's zero-based indexing).

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 372814

You can declare an array of these structs like this:

EmployeeT myEmployees[/* ... size of array ... */];

Or, if this is pure C:

struct EmployeeT myEmployees[/* ... size of array ... */];

Hope this helps!

Upvotes: 3

Related Questions