C编程从用户获取信息

时间:2015-01-23 06:07:27

标签: c string

这是我第一次使用C  我需要询问用户一些信息才能进入

 #include <stdio.h>
#include <stdlib.h>

int main(void) {
    int age;
    int i;
    char last_name[20];
    char first_name[20];
    double wight;
    char gender[1];


    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Enter your last name: ");
    fgets(last_name, 18, stdin);
        i = strlen(last_name)-1;
        if( last_name[ i ] == '\n')
            last_name[i] = '\0';

    printf("Enter your first name: ");
    fgets(first_name, 18, stdin);
            i = strlen(first_name)-1;
            if( first_name[ i ] == '\n')
                first_name[i] = '\0';

    printf("Enter your wight: ");
    scanf("%lf", &wight);
    printf("Enter your gender m/f: ");
    gets(gender);

    printf("You entered %s successfully\n ", first_name);
    return EXIT_SUCCESS;
}

如果我运行该程序  输入您的年龄:23​​岁  输入您的姓氏:输入您的名字:

我没有给我机会输入姓氏  它跳到下一行

我正在使用eclipse ide

3 个答案:

答案 0 :(得分:0)

输入age末尾的换行符char正在fgets()进行纠察 您需要在stdin缓冲区中刷新此换行符,以确保fgets()不会选择它。

使用

scanf("%d%*c",&age);

答案 1 :(得分:0)

试试这个:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int age;
    int i;
    char last_name[20];
    char first_name[20];
    double wight;
    char gender[1];


    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Enter your last name: ");
    __fpurge ( stdin );
    fgets(last_name, 18, stdin);
        i = strlen(last_name)-1;
        if( last_name[ i ] == '\n')
            last_name[i] = '\0';

    printf("Enter your first name: ");
    fgets(first_name, 18, stdin);
            i = strlen(first_name)-1;
            if( first_name[ i ] == '\n')
                first_name[i] = '\0';

    printf("Enter your wight: ");
    scanf("%lf", &wight);
    printf("Enter your gender m/f: ");
    __fpurge ( stdin );
    gets(gender);

    printf("You entered %s successfully\n ", first_name);
    return EXIT_SUCCESS;
}

答案 2 :(得分:0)

  

如果我运行程序输入您的年龄:23​​输入您的姓氏:输入您的名字:

这在代码中的第一个scanf之后发生:

scanf("%d", &age);

当您将age作为输入时,换行仍然在输入流中尾随,您需要在下一次输入之前将其清除:

scanf("%d", &age);
while(getchar() != '\n'); //eat up trailing newline
fgets(last_name, 18, stdin);

以下是您更正后的计划:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    int age;
    int i;
    char last_name[20];
    char first_name[20];
    double wight;
    char gender[1];


    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Enter your last name: ");
    while(getchar() != '\n'); //eat up trailing newline
    fgets(last_name, 18, stdin);
        i = strlen(last_name)-1;
        if( last_name[ i ] == '\n')
            last_name[i] = '\0';

    printf("Enter your first name: ");
    fgets(first_name, 18, stdin);
            i = strlen(first_name)-1;
            if( first_name[ i ] == '\n')
                first_name[i] = '\0';

    printf("Enter your wight: ");
    scanf("%lf", &wight);
    printf("Enter your gender m/f: ");
    while(getchar() != '\n'); //eat up trailing newline
    gets(gender);

    printf("You entered %s successfully\n ", first_name);
    return EXIT_SUCCESS;
}

输出:

Enter your age: 25
Enter your last name: last_Name
Enter your first name: First_Name
Enter your wight: 70
Enter your gender m/f: M
You entered First_Name successfully