的C结构不扫描所有的投入 [英] C structure not scanning all the inputs

查看:159
本文介绍了的C结构不扫描所有的投入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个C code:

I have this C code:

#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);  
}  

这是怎么发生的是它只扫描直到第二本书的作者姓名。在此之后它直接打印输出。

What is happening here is that it only scans till the author name of the second book. After that it directly prints the output.

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

Here is my input:(The first two lines are the initial printf statements)

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

什么是错在这里?此外,我们可以看到,第二本书的名字是assinged作者。

What is wrong here? Also we can see that the name of the second book is assinged to the author.

我用 GCC 在Mac OS X ML的编译器。

I'm using gcc as the compiler on Mac OS X ML.

推荐答案

<击>使用 fflush(标准输入)每个输入语句之前。此方法将清除输入缓冲区。
修改后的code将是─

Use fflush(stdin) before each input statement. This method will clear the input buffer. After the modification your code will be-

#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()这里

You can see the details about fflush() here.

更新:
在这里,scanf()的语句之后,你需要刷新输入缓冲区。因为它仅限定为输出流的fflush()方法不在这里是有用的。您可以在每个scanf()的行之后消耗了单行code部分读取行的其余部分,比如 -

UPDATED : Here after the scanf() statement you need to flush the input buffer. The fflush() method is not useful here because it is defined only for output streams. You can consume the rest of a partially-read line with a single line code after each scanf() line, like -

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

比你的code将是:

Than your code will be:

#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;
} 

OUTPUT:

OUTPUT :

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结构不扫描所有的投入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆