首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请问一个小程序,结果让人费解

2013-09-05 
请教一个小程序,结果让人费解!下面是程序:int main(void){char answer Ndouble total 0.0double v

请教一个小程序,结果让人费解!
下面是程序:
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一下

热点排行