So, I ask the user for novo->prato (a list item), but somewhere in the function call or the scanf before it, the program crashes and I can't see what is wrong with the code.
These are the declarations and the function.
typedef struct pedido pedido, *ppedido;
struct ingrediente{
char nome[15];
int id;
int qtd;
int limite;
int consumo_medio;
};
struct item{
char id[5];
int ing[10];
float qtd[10];
};
struct pedido{
char id[5];
int prioridade;
int mesa, n_pratos;
struct prato *prato[TAM];
ppedido prox;
};
struct prato{
char id[5];
};
int verifica_prato(char prato[])
{
FILE *f;
struct item aux;
int i=0, j=0;
f = fopen("menu.bin", "rb");
while((fread(&aux, sizeof(struct item), 1, f)) == 1){
if((strcmp(prato, aux.id)) == 0)
i++;
}
fclose(f);
if(i == 0){
printf("Prato nao existe no menu.\n");
return 1;
}
if(i > 1)
return 0;
}
This is where the function ia called.
for(i=0;i<novo->n_pratos;i++){
do{
printf("Introduza o ID do prato %d: ", i+1);
scanf("%s", &novo->prato[i]);
printf("%s", novo->prato[i]);
k = verifica_prato(novo->prato[i]); //this function
if(k == 0)
w = verifica_ing(novo->prato[i]->id);
}while(k != 0);
}
Inside verifica_prato()
after the line f = fopen("menu.bin", "rb")
you should check whether the file opened successfully or not.
use if( f != NULL )
Is novo->n_pratos
always less than TAM
? There could be a bounds error if it n_pratos
exceeds TAM
, because TAM
has been defined as the array size.
verifca_prato的签名不应该是int verifica_prato(struct prato),或者调用应该是verifica_prato(novo-> prato [i] - > id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.