Two bugs:
Incorrect use of scanf
:
scanf("%s",&search[10]); --> scanf("%9s", search);
Note: scanf("%9s", &search[0]);
is also fine but the above is the common way.
Incorrect string compare :
if(g[n].name == search[10]) --> if(strcmp(g[n].name, search) == 0)
Also notice that you never initialized g[3]
but fn_search
checks it.
Then this part:
else
{
printf("
Item not found.");
break;
}
means that you break the for
loop as soon as an item doesn't match. In other words: Currently you only compare against g[0]
You don't want that! Check all items before printing "Item not found".
So the for
loop should be more like:
for (n=0;n<4;n++)
{
if(strcmp(g[n].name, search) == 0)
{
printf ("
costs = NTD%.2f",g[n].costs);
printf ("
quantity = %d
",g[n].quantity);
return; // Exit function when match is found
}
}
// When execution arrives here, there was no matching element
printf("
Item not found.");
Finally:
void fn_search (struct product g[10])
^^
why ??
Either do
void fn_search (struct product g[])
or
void fn_search (struct product *g)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…