I ask for d->id to put it on the list, but if I ask for more than one, all the list items get the last one.
For example, if the for has 2 iterations and novo->prato[0] is "M1" (this is asked to the user) and novo->prato[1] is "M3", both positions stay "M3". Why?
Here is the code:
typedef struct pedido pedido, *ppedido;
typedef struct prato prato, *pprato;
struct pedido{
char id[5];
int prioridade;
int mesa, n_pratos;
pprato prato[TAM];
ppedido prox;
};
struct prato{
char id[5];
};
int verifica_prato(ppedido p,int j)
{
FILE *f;
struct item aux;
int i=0;
f = fopen("menu.bin", "rb");
if(f == NULL){printf("O sistema nao consegue aceder a memoria");return;}
while((fread(&aux, sizeof(struct item), 1, f)) == 1){
if((strcmp(p->prato[j]->id, aux.id)) == 0)
i++;
}
fclose(f);
if(i == 0){
printf("Prato nao existe no menu.\n");
return 1;
}
if(i > 1)
return 0;
}
for(i=0;i<novo->n_pratos;i++){
do{
printf("ID do prato %d: ", i+1);
scanf("%s", &d->id);
novo->prato[i] = d;
k = verifica_prato(novo,i);
}while(k != 0);
}
You are assigning the pointer to structure
dwhen you donovo->prato[i] = d,so each time you make a change to
dit would be reflected atnovo->prato[i]since all values in prato array is pointing to the same address i.e.d.I’m not exactly sure what you intend to do but one way could be to malloc
devery time in loop before asking the user for input. Remember you also need to look into ‘freeing’ the memoryprato[i]eventually.