如何在结构体数组中寻找合适的元素?
这段程序想在结构体数组中选取合适的元素并利用,通过编译,但是运行时在输入第一组信息以后屏幕就不动了,错在哪里了?
这种选取元素的方法对吗?
#include "stdio.h "
#include "bios.h "
#include "stdlib.h "
struct s
{
char name[10];
int price;
}list[]={{ "abc ",4},
{ "def ",3},
{ "ghi ",4}};
main()
{
char name[10];
int amount,i=0, price,key,j;
for(i=0;;i++)
{
printf( "input the ESC to quit,or enter any other key\n ");
fflush(stdin);
key=bioskey(0)&0xff;
if((key&27)==27)
break;
else
{
printf( "please input the No.%d goods ' price and amount\n ",i+1);
scanf( "%d%d ",list[i].name,amount);
for(j=0;strcmp(name,list[i].name)!=0;j++)
price=list[j].price*amount;
printf( "%s*%d=%d ",list[j].name,amount,list[j].price);
}
}
}
[解决办法]
for(j=0;strcmp(name,list[i].name)!=0;j++)
这里死循环了