C - scanf( "%[^n]" ) 被跳过



我想写一个小程序来学习C;这里是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int total;
char suffix[3];
struct person {
    char id[11];
    char name[21];
    char sex[7];
    int age;
    char phone[12];
};
char* number_suffix (int number) {
    int mod;
    mod = number % 10;
    switch (mod) {
        case 1:
            strcpy(suffix, "st");
            break;
        case 2:
            strcpy(suffix, "nd");
            break;
        case 3:
            strcpy(suffix, "rd");
            break;
        default:
            strcpy(suffix, "th");
            break;
    }
    return suffix;
}
void input_info (struct person info[], int total_people) {
    int counter;
    for (counter=0; counter<total_people; counter++){
        printf("%s%d%s%sn","Please input the ID(10 digits) of ", (counter+1),
                number_suffix(counter), " person: ");
        scanf("%s", info[counter].id);
        fflush(stdin);
        printf("%s%d%s%sn", "Please input the Name(20 chars) of ", (counter+1),
                number_suffix(counter), " person: ");
        scanf("%[^n]", info[counter].name);
        fflush(stdin);
        printf("%s%d%s%sn", "Please input the Sex(Male/Female) of ", (counter+1),
                number_suffix(counter), " person: ");
        scanf("%s", info[counter].sex);
        fflush(stdin);
        printf("%s%d%s%sn", "Please input the Age(1~100) of ", (counter+1),
                number_suffix(counter), " person: ");
        scanf("%d", &info[counter].age);
        fflush(stdin);
        printf("%s%d%s%sn", "Please input the Phone of ", (counter+1),
                number_suffix(counter), " person: ");
        scanf("%s", info[counter].phone);
        fflush(stdin);
    }
    printf("%sn%sn%sn%dn%sn", info[counter].id, info[counter].name, info[counter].sex, &info[counter].age, info[counter].phone);
}
int main (void) {
    printf("%sn", "Please input a number that how many people you want to record:");
    scanf("%d", &total);
    fflush(stdin);
    struct person *person_info = malloc(sizeof(struct person)*total);
    input_info(person_info, total);
    free(person_info);
    return 0;
}

当我运行它时,我发现了一些奇怪的东西。

Please input a number that how many people you want to record:
1
Please input the ID(10 digits) of 1th person:
A01
Please input the Name(20 chars) of 1th person:
Please input the Sex(Male/Female) of 1th person:
Male
Please input the Age(1~100) of 1th person:
32
Please input the Phone of 1th person:
1224464
[empty line]
[empty line]
[empty line]
1926234464
[empty line]

该程序在运行时是否scanf("%[^n]", info[counter].name);跳过此行?

为什么,是什么原因造成的?

根据 C 标准,fflush(stdin) 是未定义的,但它适用于某些实现。但最好避免使用它,因为它不可移植,并且可能会调用未定义的行为。

要解决此问题,请将所有fflush(stdin)替换为

int c; /* Declare it once */
while((c = getchar()) != 'n' && c != EOF); /* Discards everything until a newline character or EOF */

另一个问题是

printf("%sn%sn%sn%dn%sn", info[counter].id, info[counter].name, info[counter].sex, &info[counter].age, info[counter].phone);

它应该是

printf("%sn%sn%sn%dn%sn", info[counter].id, info[counter].name, info[counter].sex, info[counter].age, info[counter].phone);

并应放置在for。否则,您将调用未定义的行为,因为

  1. 您通过了期望int%d int*
  2. 访问超出分配的内存段的无效内存位置。

此外,正如其他人所说,counter + 1传递给number_suffix.

  1. 问题出在您的扫描模式上。使用 " %[^n]" 而不是 "%[^n]" 不捕获n(在上一个数据输入之后)
  2. counter + 1传递给number_suffix

如何理解指针、结构体、malloc、函数参数之间的关系?

阅读 O'Reilly Media 的 Understanding and Use C Pointers

相关内容

  • 没有找到相关文章

最新更新