C 跳过中间的一些扫描
在这个程序的第二次和第四次扫描得到跳过,不知道原因。能告诉一些人原因吗C 跳过中间的一些扫描,c,char,scanf,C,Char,Scanf,在这个程序的第二次和第四次扫描得到跳过,不知道原因。能告诉一些人原因吗 #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 he
#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");
}
#包括
main()
{
智力年龄;
性别、地位、城市;
printf(“输入人员年龄”);
scanf(“\n%d”和年龄);
printf(“输入性别”);
scanf(“%c”、&sex);
printf(“输入健康状态”);
scanf(“%c”、&status);
printf(“该人居住的城市或村庄”);
scanf(“%c”和城市);
如果在使用scanf()读取字符时((年龄>25)和(&&(年龄25和年龄25和年龄),它会在输入缓冲区中留下一个换行字符
更改:
scanf("%c",&sex);
printf("enter the health status");
scanf("%c",&status);
printf("where the person stay city or village");
scanf("%c",&city);
致:
注意scanf格式字符串中的前导空格,它告诉scanf忽略空格
或者,您可以使用来使用换行符
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()读取字符时,它会在输入缓冲区中留下一个换行字符
更改:
scanf("%c",&sex);
printf("enter the health status");
scanf("%c",&status);
printf("where the person stay city or village");
scanf("%c",&city);
致:
注意scanf格式字符串中的前导空格,它告诉scanf忽略空格
或者,您可以使用来使用换行符
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
相同的问题,因此,我使用字符串。我将使用:
#include<stdio.h>
main()
{
int age;
char sex[3],status[3],city[3];
printf("Enter the persons age \n");
scanf("\n%d",&age);
printf("enter the gender\n");
gets(sex);
printf("enter the health status");
gets(status);
printf("where the person stay city or village");
gets(city);
if(((age>25)&&(age<35))&&(sex[0]=='m')&&(status[0]=='g')&&(city[0]=='c'))
printf("42");
else if(age>25&&age<35&&sex[0]=='f'&&status[0]=='g'&&city[0]=='c')
printf("31");
else if(age>25&&age<35&&sex[0]=='m'&&status[0]=='b'&&city[0]=='v')
printf("60");
else
printf("no");
}
每次使用age
时都要使用atoi
,因为atoi
将字符字符串转换为整数(int)。我总是遇到与使用scanf
相同的问题,因此,我改用字符串。我会使用:
#include<stdio.h>
main()
{
int age;
char sex[3],status[3],city[3];
printf("Enter the persons age \n");
scanf("\n%d",&age);
printf("enter the gender\n");
gets(sex);
printf("enter the health status");
gets(status);
printf("where the person stay city or village");
gets(city);
if(((age>25)&&(age<35))&&(sex[0]=='m')&&(status[0]=='g')&&(city[0]=='c'))
printf("42");
else if(age>25&&age<35&&sex[0]=='f'&&status[0]=='g'&&city[0]=='c')
printf("31");
else if(age>25&&age<35&&sex[0]=='m'&&status[0]=='b'&&city[0]=='v')
printf("60");
else
printf("no");
}
并在每次使用age
时使用atoi
,因为atoi
将字符字符串转换为整数(int)