0

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

Tom
  • 2,973
  • 3
  • 28
  • 32
Gvegas222
  • 213
  • 2
  • 4
  • 11
  • 4
    C or C++? Pick one. For C++ this is terrible code. – Kerrek SB Feb 01 '12 at 03:43
  • 7
    This is really basic C++. I recommend you pick up a [good introductory C++ book](http://stackoverflow.com/q/388242/46642). – R. Martinho Fernandes Feb 01 '12 at 03:43
  • 4
    In light of this and [your last question](http://stackoverflow.com/questions/9090098/how-to-define-a-struct-in-c), I really can't emphasize R. Martinho's comment enough. It's extraordinarily difficult to learn a language like C++ through a series of Stack Overflow questions. A good book is mandatory, lest you cement bad habits and develop fundamental misunderstandings early on in your programming career. Also, do be careful not to mix C and C++: they are emphatically *not* the same language. – Cody Gray - on strike Feb 01 '12 at 03:48
  • 1
    I hope you're aware that `char name[MAXSIZE]` *is* an array... If not, you're now and the rest should be clear. – Xeo Feb 01 '12 at 03:51
  • Go to the library or bookshop and get a book. – Ed Heal Feb 03 '12 at 07:54

3 Answers3

3

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!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

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).

Chiara Coetzee
  • 4,201
  • 1
  • 24
  • 20
  • 3
    Nitpick: Not static. Automatic. I know you mean the opposite of a dynamically allocated array, but it's not static. :) – Xeo Feb 01 '12 at 03:53
  • @Xeo: I thought the same thing, but then found [this SO question](http://stackoverflow.com/questions/2672085/c-static-array-vs-dynamic-array). – Jesse Good Feb 01 '12 at 03:56
  • @Xeo no one said array of static storage duration :P Static is way too overloaded a word. – R. Martinho Fernandes Feb 01 '12 at 03:59
  • @R. Martinho: It's one of my pet peeves, exactly because of that overloaded usage. :) – Xeo Feb 01 '12 at 04:01
  • @Jesse: That question and most of the answers abuse the terminology to the point of actually being wrong. Statically-sized arrays are not necessarily on the stack, if they have static storage duration they will go somewhere else (usually the data segment, but the standard doesn't say). – Ben Voigt Feb 01 '12 at 04:06
  • @Xeo: It might not be automatic, if it's global. Or a class member. – Ben Voigt Feb 01 '12 at 04:07
  • @BenVoigt: Thanks, I did feel the same way. Sometimes I wonder what people are thinking when they upvote an answer. – Jesse Good Feb 01 '12 at 04:17
  • To avoid any issues I changed it to read "fixed-size." But I think referring to arrays whose sizes are fixed at compile-time as "static" is standard terminology. It doesn't imply anything about its storage class or lifetime. – Chiara Coetzee Feb 01 '12 at 04:52
0
int n;
cout<<"Enter number of records: ";
cin>>n
employeeT *ptr_e=new employeeT[n]
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112