Problem with struct: "Expected Expression before"

Asked

Viewed 1,151 times

1

The following problem

expected Expression before 'eqp'

at the line of the data function where the value is read and I could not identify the reason for this.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

typedef struct equipamento{
int codigoequipamento;
int tempoativo;
float potencia;
float consumonomes;
char equipamento[30];
}eqp;
typedef struct equipamento eqp;

void menu(void);
void dados(void);
int main(){

   setlocale(LC_ALL, "Portuguese"); 
   int resp=0;
   do{
       menu();
       scanf ("%d",&resp);
       if(resp == 1){ // Para caso deseje-se cadastrar
        dados();

       }if(resp == 2){// Caso deseje-se consultar oq já foi cadastrado

       }if(resp == 3){
          break;
       }if((resp > 3) || (resp < 1)){
          printf("Valor invalido\n");
       }
       }while (resp != 3);
       return 0;
 }
void menu(void){  //menu
  printf("---------------------------------------\n");
  printf("| Sessão de cadastro de equipamentos  |\n");
  printf("---------------------------------------\n");
  printf("Digite 1 para cadastrar\n");
  printf("Digite 2 para consultar os cadastros\n");
  printf("Digite 3 para sair\n");
}

void dados(void){ // Função para ler dados

  printf("Entre com o codigo do equipamento: ");
  scanf("%d",&eqp.codigoequipamento);
}

1 answer

0


There are several errors there and I tried to correct the main ones (I kept some things I would do differently in real case), maintaining a different organization and nomenclature, removing what is redundant and unnecessary and switching to what is most suitable.

The error in focus is that you are trying to manipulate the type and not a variable. You have to create the variable. I imagine I wanted to create in the main code and pass as parameter to use in the function that will manipulate it, so that’s what I did.

I suggest paying attention to every detail of what I changed (up to white spaces), in general it is not by case, it is all thought out.

#include <stdio.h>
#include <locale.h>

typedef struct {
    int codigo;
    int tempoativo;
    float potencia;
    float consumonomes;
    char nome[31];
} Equipamento;

void menu(void) {
    printf("---------------------------------------\n");
    printf("| Sessão de cadastro de equipamentos  |\n");
    printf("---------------------------------------\n");
    printf("Digite 1 para cadastrar\n");
    printf("Digite 2 para consultar os cadastros\n");
    printf("Digite 3 para sair\n");
}

void dados(Equipamento *equipamento) {
    printf("Entre com o codigo do equipamento: ");
    scanf("%d", &equipamento->codigo);
}

int main() {
   setlocale(LC_ALL, "Portuguese"); 
   int resp = 0;
   Equipamento equipamento;
   do {
       menu();
       scanf("%d", &resp);
       switch (resp) {
       case 1:
           dados(&equipamento);
           break;
       case 2:
           break;
       }
       if (resp > 3 || resp < 1) printf("Valor invalido\n");
    } while (resp != 3);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • When reading the data you used the "->", could you tell me what concept this involves? p.s: It helped a lot, thank you.

  • '->' is used whenever the value on the right is an address, and'&' represents "address"; so when using '&' before the variable as it was done in "&equipment" '->' should be used with "&equipment->code". @Carolinemarx

  • @CarolineMarx https://answall.com/q/325987/101 e https://answall.com/a/51204/101

Browser other questions tagged

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