一些scanf函数中的跳跃 [英] skipping of some scanf in between
本文介绍了一些scanf函数中的跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此计划的第二和第四scanf函数得到跳绳,不知道原因。可一些请告诉原因?
#包括LT&;&stdio.h中GT;
主要()
{
INT年龄;
焦炭性别,地位,城市;
的printf(请输入个人的年龄\\ n);
scanf函数(\\ N%D,&安培;年龄);
的printf(请输入性别\\ n);
scanf函数(%C,&安培;性);
的printf(进入健康状态);
scanf函数(%C,&安培;状态);
的printf(里的人呆在城市或村庄);
scanf函数(%C,&安培;市);
if(((age>25)&&(age<35))&&(sex=='m')&&(status=='g')&&(city=='c'))
的printf(42);
否则,如果(年龄&GT; 25安培;&安培;年龄&LT; 35安培;&安培;性=='F'和;&放大器;状态=='G'和;&安培;城市=='C')
的printf(31);
否则如果(年龄→25&放大器;&放大器;年龄&所述35安培;&放大器;性=='M'和;&放大器;状态=='B'和;&放大器;城市=='V')
的printf(60);
其他
输出(无); }
解决方案
在阅读使用scanf()的,它使输入缓冲区换行字符。字符
修改
scanf函数(%C,&安培;性);
的printf(进入健康状态);
scanf函数(%C,&安培;状态);
的printf(里的人呆在城市或村庄);
scanf函数(%C,&安培;市);
到
scanf函数(%C,&安培;性);
的printf(进入健康状态);
scanf函数(%C,&安培;状态);
的printf(里的人呆在城市或村庄);
scanf函数(%C,&安培;市);
注意在scanf函数的格式字符串中的前导空格告诉scanf函数忽略空格。
另外,你可以使用 的getchar() 供消费的换行字符。
scanf函数(%C,&安培;性);
的getchar();
的printf(进入健康状态);
scanf函数(%C,&安培;状态);
的getchar();
的printf(里的人呆在城市或村庄);
scanf函数(%C,&安培;市);
的getchar();
In this program the second and forth scanf get skipping , don't know the reason . can some please tell the reason ?
#include<stdio.h>
main()
{
int age;
char sex,status,city;
printf("Enter the persons age \n");
scanf("\n%d",&age);
printf("enter the gender\n");
scanf("%c",&sex);
printf("enter the health status");
scanf("%c",&status);
printf("where the person stay city or village");
scanf("%c",&city);
if(((age>25)&&(age<35))&&(sex=='m')&&(status=='g')&&(city=='c'))
printf("42");
else if(age>25&&age<35&&sex=='f'&&status=='g'&&city=='c')
printf("31");
else if(age>25&&age<35&&sex=='m'&&status=='b'&&city=='v')
printf("60");
else
printf("no");
}
解决方案
When reading chars using scanf(), it leaves a newline char in the input buffer.
Change :
scanf("%c",&sex);
printf("enter the health status");
scanf("%c",&status);
printf("where the person stay city or village");
scanf("%c",&city);
to:
scanf(" %c",&sex);
printf("enter the health status");
scanf(" %c",&status);
printf("where the person stay city or village");
scanf(" %c",&city);
Notice the leading whitespace in scanf's format string which tells scanf to ignore whitespaces.
Alternatively, you can use getchar() for consuming the newline chars.
scanf("%c",&sex);
getchar();
printf("enter the health status");
scanf("%c",&status);
getchar();
printf("where the person stay city or village");
scanf("%c",&city);
getchar();
这篇关于一些scanf函数中的跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文