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

5
  • 4
    C or C++? Pick one. For C++ this is terrible code. Commented Feb 1, 2012 at 3:43
  • 7
    This is really basic C++. I recommend you pick up a good introductory C++ book. Commented Feb 1, 2012 at 3:43
  • 4
    In light of this and your last question, 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. Commented Feb 1, 2012 at 3: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. Commented Feb 1, 2012 at 3:51
  • Go to the library or bookshop and get a book. Commented Feb 3, 2012 at 7:54

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

Nitpick: Not static. Automatic. I know you mean the opposite of a dynamically allocated array, but it's not static. :)
@Xeo: I thought the same thing, but then found this SO question.
@Xeo no one said array of static storage duration :P Static is way too overloaded a word.
@R. Martinho: It's one of my pet peeves, exactly because of that overloaded usage. :)
@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).
|
0
int n;
cout<<"Enter number of records: ";
cin>>n
employeeT *ptr_e=new employeeT[n]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.