请教一个小程序,结果让人费解!
下面是程序:
int main(void)
{
char answer = 'N';
double total = 0.0;
double value = 0.0;
int count = 0;
printf("\nthis program calculates the average of any number of values.");
for(;;)
{
printf("\nenter a number:");
scanf("%lf",&value);
total +=value;
++count;
printf("\ndo you want continue?(Y or N):");
scanf("%c",&answer);
if(tolower(answer)=='n')
break;
}
printf("\nthe average is %.2lf\n",total/count);
system("pause");
return 0;
}
程序编译执行后,提示输入数字,输入了,然后正常提示do you want continue?(Y or N):,这句之后就直接跳到enter a number:了,没有让我输入字符Y/N,怎么回事啊,逻辑上看也没问题啊,请各位指点迷津!
[解决办法]
回车键 被 读到 answer里去了,flush一下