Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 跳过中间的一些扫描_C_Char_Scanf - Fatal编程技术网

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)