I am having some trouble comparing two variables, hope someone could help.
Basically what i do is:
printf("\n\n +* Introduza o %d Prato:", i + 1);
scanf("%s", &pratos_novo->prato);
ver=verifica_prato(pratos_novo->prato);
if(ver == 1)
/*PROCESS*/
I put M1 when asked because the it does exist in .bin file
function call:
int verifica_prato(char* p) {
k = (struct item*) malloc(sizeof(item));
while((fread(k, 1, sizeof(item), f)) != NULL) {
if((*k).id == p){
fclose(f);
return 1;
}
}
fclose(f);
return 0;
}
My goal is to return 1 when i put M1.
struct item:
struct item{
char id[5];
int ing[10];
float qtd[10];
};
The problem is here : (*k).id==p), the values are never the same, and I am 100% sure that M1 does exist in the .bin file.
You are comparing pointers when you need to compare strings.
Replace
if((*k).id==p)withif( strcmp( (*k).id, p) == 0 )to compare the strings.