Problem with nested structures

0

I'm doing an exercise where I have to load student data. I do it using functions using structures. I have a syntax problem of how to save a data in a nested structure.

I tried to do it like this:

a[i].fe.dia;

And I get the following error:

  

request for member 'dia' in '((+ (((unsigned int) i) * 144u)) + a) - > students :: faith', which is of non-class type 'date [2] '

and I also tried this way:

a.fe[i].dia;

and when I do it, I get the following error:

  

'faith' has not been declared

But in neither of the two ways does it work for me. I do not know if I'm doing it wrong or I lacked some statement or something to make it work.

struct fecha
{ 
   int dia, mes, anio;
};

struct alumnos
{
   int legajo [10];
   char apnom [35];
   char direc [40];
   struct fecha fe [2];  /* fecha nacimiento y fecha ingreso institución */
   int edad;
};

void carga_alu (struct alumnos [ ], int *); 


int main()
{

struct alumnos alum[100];
int cantalum;

carga_alu(alum,&cantalum); 




system("PAUSE");
return 0;
}


void carga_alu (struct alumnos a[ ], int *c)
{
char rta='s';
int i=0,cont=0;
while (rta=='s')
   { printf("Ingrese el nombre y apellido del alumno \n");
     gets(a[i].apnom);
     printf("Ingrese el legajo del alumno \n");
     scanf("%d",&a[i].legajo);
     printf("Ingrese la direccion del alumno \n");
     gets(a[i].direc);
     printf("Ingrese la fecha de nacimiento del alumno: DIA \n");
     scanf("%d", &a[i].fe.dia);
     printf("Ingrese la fecha de nacimiento del alumno: MES \n");
     scanf("%d", &a[i].fe.mes);
     printf("Ingrese la fecha de nacimiento del alumno: ANIO \n");
     scanf("%d", &a[i].fe.anio);
     printf("Ingrese la fecha de ingreso a la institucion del alumno: DIA           \n");
     scanf("%d", &a[i].fe.dia);
     printf("Ingrese la fecha de ingreso a la institucion del alumno: MES  \n");
     scanf("%d", &a[i].fe.mes);
     printf("Ingrese la fecha de ingreso a la institucion del alumno: ANIO \n");
     scanf("%d", &a[i].fe.anio);
     cont=cont+1;
     i=i+1;
     printf("Desea cargar otro alumno? S o N");
     scanf("%c",rta);} }
    
asked by Maca Igoillo 31.01.2017 в 14:45
source

1 answer

3

Various errors:

while (rta='s')

There you are doing an assignment instead of a comparison, then your code will never leave the loop. The instruction should look like this:

while (rta=='s')

And with the subject of the indexes, a.fe[i] comment you the following:

  • a is an array, and the index i must act on a , the same as happens with a[i].legajo .
  • fe is another arrangement with two elements: fe[0] for the date of birth and fe[1] for the date of entry.

So to access the day of the date of birth you should do the following:

a[i].fe[0].dia

Also be informed that scanf requires that the variables are passed as pointers to be able to modify their values, then the code should be like this:

printf("Ingrese la fecha de nacimiento del alumno: DIA \n");
scanf("%d", &a[i].fe[0].dia);
printf("Ingrese la fecha de nacimiento del alumno: MES \n");
scanf("%d", &a[i].fe[0].mes);
printf("Ingrese la fecha de nacimiento del alumno: ANIO \n");
scanf("%d", &a[i].fe[0].anio);
printf("Ingrese la fecha de ingreso a la institucion del alumno: DIA\n");
scanf("%d", &a[i].fe[1].dia);
printf("Ingrese la fecha de ingreso a la institucion del alumno: MES  \n");
scanf("%d", &a[i].fe[1].mes);
printf("Ingrese la fecha de ingreso a la institucion del alumno: ANIO \n");

The comment about scanf is also applicable for the variables of a :

scanf("%d",&a[i].legajo);

And for the last instruction in the loop:

scanf("%c",&rta);

Why is it not necessary to put & in the case of calls to gets ?

Because the type char[] is implicitly a pointer, then to gets you are already passing a pointer to it.

And one last thing:

struct alumnos
{
   int legajo [10];
};

The variable legajo looks like it's not an arrangement, then it should look like this:

struct alumnos
{
   int legajo;
};

And, to top it off, everything points to the variable i should be replaced by c . You currently use i to perform the count of records added to the list ... but the function should return that result in c . The variable cont could also disappear:

void carga_alu (struct alumnos a[ ], int *c)
{
  char rta='s';
  while (rta=='s')
  {
    printf("Ingrese el nombre y apellido del alumno \n");
    gets(a[*c].apnom);
    printf("Ingrese el legajo del alumno \n");
    scanf("%d",&a[*c].legajo);
    printf("Ingrese la direccion del alumno \n");
    gets(a[*c].direc);
    printf("Ingrese la fecha de nacimiento del alumno: DIA \n");
    scanf("%d", &a[*c].fe[0].dia);
    printf("Ingrese la fecha de nacimiento del alumno: MES \n");
    scanf("%d", &a[*c].fe[0].mes);
    printf("Ingrese la fecha de nacimiento del alumno: ANIO \n");
    scanf("%d", &a[*c].fe[0].anio);
    printf("Ingrese la fecha de ingreso a la institucion del alumno: DIA\n");
    scanf("%d", &a[*c].fe[1].dia);
    printf("Ingrese la fecha de ingreso a la institucion del alumno: MES  \n");
    scanf("%d", &a[*c].fe[1].mes);
    printf("Ingrese la fecha de ingreso a la institucion del alumno: ANIO \n");
    scanf("%d", &a[*c].fe[1].anio);
    *c++;
    printf("Desea cargar otro alumno? S o N");
    scanf("%c",&rta);
  }
}

Now, it is critically important that you initialize the variable cantalum :

int main()
{
  struct alumnos alum[100];
  int cantalum = 0;

  carga_alu(alum,&cantalum);
    
answered by 31.01.2017 / 14:50
source