C 结构不扫描所有输入

标签 c structure scanf fgets gets

我有这个 C 代码:

#include "stdio.h"

main()
{
    struct books
    {
        char name[100],author[100];
        int year,copies;
    }book1,book2;

    printf("Enter details of first book\n");
    gets(book1.name);
    gets(book1.author);
    scanf("%d%d",&book1.year,&book1.copies);

    printf("Enter details for second book\n");
    gets(book2.name);
    gets(book2.author);
    scanf("%d%d",&book2.year,&book2.copies);

    printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
    printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);  
}  

这里发生的事情是它只扫描到第二本书的作者姓名。之后它直接打印输出。

这是我的输入:(前两行是初始 printf 语句)

Enter details of first book
warning: this program uses gets(), which is unsafe.
the c programmign laguagne
dfadsda
3432
23
Enter details for second book
ruby on rails
mark hammers  

之后它直接打印输出:

the c programmign laguagne
dfadsda
3432
23

ruby on rails
0
0

这里出了什么问题?我们还可以看到第二本书的名字被分配给作者。

我使用 gcc 作为 Mac OS X ML 上的编译器。

最佳答案

使用fflush(stdin)在每个输入语句之前。该方法将清除输入缓冲区。 修改后您的代码将是 -

#include "stdio.h"

int main()
{
    struct books
    {
        char name[100],author[100];
        int year,copies;
    }book1,book2;

    printf("Enter details of first book\n");
    gets(book1.name);
    fflush(stdin);

    gets(book1.author);
    fflush(stdin);

    scanf("%d%d",&book1.year,&book1.copies);
    fflush(stdin);

    printf("Enter details for second book\n");
    gets(book2.name);
    fflush(stdin);

    gets(book2.author);
    fflush(stdin);
    scanf("%d%d",&book2.year,&book2.copies);

    printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
    printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);  
    return 0;
} 

您可以查看 fflush() 的详细信息here .

更新: 在 scanf() 语句之后,您需要刷新输入缓冲区。 fflush() 方法在这里没有用,因为它仅为输出流定义。您可以在每个 scanf() 行之后使用单行代码来使用部分读取的行的其余部分,例如 -

while((c = getchar()) != '\n' && c != EOF);

你的代码将是:

#include "stdio.h"

int main()
{
    struct books
    {
        char name[100],author[100];
        int year,copies;
    }book1,book2;
    char c;
    printf("Enter details of first book\n");
    gets(book1.name);
    gets(book1.author);

    scanf("%d%d",&book1.year,&book1.copies);
    while((c = getchar()) != '\n' && c != EOF);

    printf("Enter details for second book\n");
    gets(book2.name);
    gets(book2.author);
    scanf("%d%d",&book2.year,&book2.copies);
    while((c = getchar()) != '\n' && c != EOF);

    printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
    printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);  
    return 0;
} 

输出:

Enter details of first book
warning: this program uses gets(), which is unsafe.
sadsadas
asa
12
34
Enter details for second book
zxczxc
sds
23
22
sadsadas
asa
12
34
zxczxc
sds
23
22

关于C 结构不扫描所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19376077/

相关文章:

c - strcat() 是否像 strcat_s() 一样返回错误?

c++ - 在类公共(public)方法中创建一个新结构

c - 结构程序未按预期工作(错误)

c - 从 SCANF 返回 EOF

c - scanf的宽度规范和scanf_s的区别

c - 如何多次读取文件 C

c - 使用 Fork 的递归斐波那契数列(C 语言)

c - 从 C 中的文本文件中删除换行符

c - C 中的文件、访问指针、读取和写入文件

C fscanf - 格式化输入