¿Hacer una serie de estructuras?

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?

Gracias chicos

preguntado el 01 de febrero de 12 a las 03:02

C or C++? Pick one. For C++ this is terrible code. -

This is really basic C++. I recommend you pick up a buen libro introductorio de C ++. -

In light of this and tu ultima pregunta, 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 no the same language. -

I hope you're aware that char name[MAXSIZE] is an array... If not, you're now and the rest should be clear. -

Go to the library or bookshop and get a book. -

3 Respuestas

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 ... */];

¡Espero que esto ayude!

Respondido 01 Feb 12, 07:02

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

Respondido 01 Feb 12, 08:02

Nitpick: Not static. Automatic. I know you mean the opposite of a dynamically allocated array, but it's not static. :) - Xeo

@Xeo: I thought the same thing, but then found esta pregunta SO. - Jesse bueno

@Xeo no one said array of static storage duration :P Static is way too overloaded a word. - R. Martinho Fernandes

@R. Martinho: It's one of my pet peeves, exactly because of that overloaded usage. :) - Xeo

@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

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

Respondido 03 Feb 12, 11:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.