#include <stdio.h>
#include <ctype.h>
void main()
{
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 value:");
scanf("%lf",&value);
total+=value;
++count;
printf("Do you want to enter another value?(Y or N):");
scanf("%c",&answer); //没有执行呀
if(tolower(answer)=='n')
break;
}
printf("\nThe average is %.2lf\n",total/count);
}
scanf("%c",&answer);
一般都会停下来让我们输入的!可是有直接跳回去循环了!
这是因为在上一次使用scanf后没有清空输入缓存, 这样你再次使用scanf的时候函数就可能会认为你已经输入过了。
解决这一问题的最简单办法是在接收字符的scanf的控制符"%c"中的%前加一个空格写成" %c",把前一次输入遗留在输入缓冲区里的所有广义空格(' '、'\t'、'\n'等)都吸收掉。
扩展资料:
函数原型
int scanf(const char * restrict format,...);
函数 scanf() 是从标准输入流stdin(标准输入设备,一般指向键盘)中读内容的通用子程序,可以说明的格式读入多个字符,并保存在对应地址的变量中。
函数的第一个参数是格式字符串,它指定了输入的格式,并按照格式说明符解析输入对应位置的信息并存储于可变参数列表中对应的指针所指位置。
参考资料来源:百度百科-scanf (计算机语言函数)