【发布时间】:2015-02-12 18:42:24
【问题描述】:
程序第一次运行正常,但是如果我们要添加另一本书的详细信息,我输入第一个属性后会出现分段错误
int main()
{
float count_tot=0,profit_tot=0;
char option='y';
fflush(stdin);
struct book b;
while(option!='n')
{
b=getinput();
display(b);
b.need= calcneed(b);
b.profit=calcprofit(b);
printf("Need To Order:%d\n",b.need);
printf("Total Cost:%f\n",(b.need-b.qtyonhand)*b.price_sin);
printf("do another book(Y/n)");
scanf("%c",&option);
option=getchar();
count_tot+=(b.need-b.qtyonhand)*b.price_sin;
profit_tot+=b.profit;
}
drawline();
printf("TOTAL PROFIT:%f\n",profit_tot);
printf("TOTAL COST:%f\n",count_tot);
return(0);
}
struct book getinput
{
struct book b;
scanf("%d",&b.book_code);
...
...
} //the function contains a number of scanf functions
【问题讨论】:
-
缩进很可怕,很容易遵循代码流
-
请贴出
struct book的定义和getinput的完整代码。
标签: c function memory segmentation-fault structure