【发布时间】:2013-01-16 08:15:54
【问题描述】:
这是 main() 的代码:
int main (void)
{
float acres[20];
float bushels[20];
float cost = 0;
float pricePerBushel = 0;
float totalAcres = 0;
char choice;
int counter = 0;
for(counter = 0; counter < 20; counter++)
{
printf("would you like to enter another farm? ");
scanf("%c", &choice);
if (choice == 'n')
{
printf("in break ");
break;
}
printf("enter the number of acres: ");
scanf("%f", &acres[counter]);
printf("enter the number of bushels: ");
scanf("%f", &bushels[counter]);
}
return 0;
}
程序每次运行第一次 scanf 时都可以正常运行,但在第二次通过循环时 scanf 输入字符不会运行。
【问题讨论】:
-
查看这篇文章中的答案,它可能会有所帮助:stackoverflow.com/questions/1669821/…
-
@MichaelOliver:Jonathan Leffler 的解决方案似乎比公认的解决方案更好。 :)