在什么情况下要清空输入缓冲区?
下面是我写的一个计算升高的程序,我开始没加while((getchar=())!='\n'),然后程序运行到一半,就会自动地输出下面的部分。请教了下别人,说因为没清空缓冲区,可是在什么情况下需要清空输入缓冲区,还有输入缓冲区不是会被下次的输入覆盖吗?求教程序代码:
#include<stdio.h> main() { char sex,sports,diet; int h,fh,mh,c; printf("Hello,welcome to the test of how tall will you be\n"); printf("how tall are you father?\n"); scanf("%d",&fh); printf("how tall are you mather?\n"); scanf("%d",&mh); while((c=getchar())!='\n'); printf("May I ask you gender?please print F or M\n"); scanf("%c",&sex); while((c=getchar())!='\n'); printf("Do you like playing sports?Y or N\n"); scanf("%c",&sports); while((c=getchar())!='\n'); printf("Do you have healthy habit?Y or N\n"); scanf("%c",&diet); while((c=getchar())!='\n'); if(sex=='M'||sports=='m') { h=(fh+mh)*0.54; if(sports=='Y'||sports=='y') h=h*1.02; if(diet=='Y'||diet=='y') h=h*1.015; printf("You will be %d\n",h); } else if(sex=='F'||sex=='f') { h=(fh*0.923+mh)/2; if(sports=='Y'||sports=='y') h=h*1.02; if(diet=='Y'||diet=='y') h=h*1.015; printf("You will be %d\n",h); while(getchar()!='\n'); getchar(); } return 0; }