输出的时候,为什么直接就跳过Do you like sport Y/N的输入环节?

#include "stdafx.h"
#include<stdio.h>
void main()
{
float faHeight,moHeight,myHeight; 
char sex,sport,diet;

printf("You are F or M:\n");
scanf("%c",&sex); 

printf("DO you like sport Y/N:\n");
scanf("%c",&sport);

printf("DO you diet Y/N:\n");
scanf("%c",&diet);

printf("please check in your father height:\n");
scanf("%f",&faHeight);

printf("please check in your mother height:\n");
scanf("%f",&moHeight);

if(sex=='M')
myHeight=(faHeight+moHeight)*0.54;

else
myHeight=(faHeight*0.923 + moHeight)/2;

if(sport=='Y')
myHeight=myHeight*(1+0.02);

if(diet=='Y')
myHeight=myHeight*(1+0.015);
printf("myHeight is %f \n",myHeight);
}

喵喵时光机
浏览 69回答 2
2回答

慕哥9229398

犯堆栈溢出等的安全检查。传统C函数,scanf()按现代观点,就属于不安全函数。。所以MS 定义了scanf_s()函数,建议用它代替scanf这只是个警告信息,你初学C语言,建议忽略它。

料青山看我应如是

从键盘输入的每个字符都会送到输入缓冲流中,scanf函数从输入缓冲流中依次读数据,第一次输入的回车符也在输入缓冲流中,而scanf函数遇到空格或换行符就结束,所以第二次scanf时还是从输入缓冲流中的回车符开始读,读到即结束,然后执行下一个语句。解决方法:在每个scanf语句后加一句getchar();因为getchar()函数不会忽略回车符和空格符,也就是说getchar()函数会将回车符或空格符读出,然后读下一个字符。这样下一次scanf()时就是从回车符的下一个字符开始读了。
打开App,查看更多内容
随时随地看视频慕课网APP