Error request for Member 'salario' in Something not a Structure or Union

Asked

Viewed 31 times

-1

Guys, I need help with the sum of variables in a struct. the program does not compile and displays this error [Error] request for Member 'salario' in Something not a Structure or Union. The objective is to add the values entered by the user in the salary field.

#include <stdio.h>
#include <stdlib.h>
struct funcionario
{
    char nome[40];
    float salario;
};

struct funcionario cad_func[2];
int main(void) {

    int i;

    printf("INSIRA SEUS DADOS.\n\n\n");

    for(i=0; i<2; i++)
    {
        printf("nome: ");
        fflush(stdin);
        gets(cad_func[i].nome);

        printf("salario: ");
        scanf("%f", &cad_func[i].salario);

        printf("\n\n");
    }

    system("cls");

    printf("------------SEGUE ABAIXO NOME E SALARIO------------\n\n");

    for(i=0; i<2; i++){
        printf("Nome: %s\n", cad_func[i].nome);
        printf("Salario: %f\n\n\n", cad_func[i].salario);
    }

    float totalSalario;

    totalSalario = cad_func.salario[0] + cad_func.salario[1];

    printf("Total: %f", totalSalario);

    return 0;
}

Thank you!

1 answer

0


Note that when performing the calculation of totalSalario you try to access the salario of manure funcionario as if it were a array:

totalSalario = cad_func.salario[0] + cad_func.salario[1];

But actually its variable cad_func is a array, so access to indexes actually needs to be done in the cad_func and not in the salario:

totalSalario = cad_func[0].salario + cad_func[1].salario;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.