为什么scanf()不等待键盘输入? [英] Why isn't scanf( ) waiting for input from keyboard?

查看:477
本文介绍了为什么scanf()不等待键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的C代码.

#include<stdio.h>

int main()
{
    //Declaring structure book.
    struct book
    {
            char name;
            float price;
            int pages;
    };

    struct book b[5];

    int i;
    //Below loop takes the info if 5 books from user
    for (i=0; i<5; i++)
    {
            printf("Enter name, price, and pages: ");
            fflush( stdin );
            scanf("%c%f%d",&b[i].name,&b[i].price,&b[i].pages);
    }
        return 0;
}

但是,当我编译运行时,会发生一些奇怪的事情.

However when I compile and run, something strange happens.

-bash-4.1$ ./a.out
Enter name, price, and pages: A 23 34
Enter name, price, and pages: B 34 54
Enter name, price, and pages: Enter name, price, and pages: C 56 78
Enter name, price, and pages: -bash-4.1$

您可以看到,当i = 2时,scanf()不会等待键盘.然后,当i = 3时,scanf()等待键盘输入.同样在i = 4中,scanf()不会等待键盘输入.

You can see that when i = 2, scanf() does NOT wait for keyboard. Then when i = 3, scanf() waits for keyboard input. Again in i=4, scanf() does NOT wait for keyboard input.

我想我已经用过

fflush(stdin);

在正确的位置.我不希望返回键在下一个scanf()中的缓冲区中.

in the correct place. I don't want the return key to be in the buffer in the next scanf().

要调试,我尝试不使用fflush(stdin)并查看发生了什么.但是,即使没有fflush(stdin),当我运行程序时也会发生同样的事情.因此,我猜fflush(stdin)不会导致此问题.

To debug, i tried not to use fflush(stdin) and see what heppens. But even without the fflush(stdin), same thing happens when i run the program. So I guess fflush(stdin) is not causing this problem.

请任何人指出,我的程序出了什么问题?

Please anyone point out, where my program is going wrong?

谢谢.

推荐答案

C11标准解释了%c的工作原理:

The C11 standard explains how %c works:

§7.21.6.2/8输入空格字符(由isspace函数指定) 除非规范包含[cn,否则将跳过 说明符.

§7.21.6.2/8 Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

因此,Enter键产生的换行符被%c占用.您可以通过在%c:

As such, the linefeed produced by the enter key is consumed by %c. You can fix this by adding a space before %c:

§7.21.6.2/5由空格字符组成的指令为 通过读取输入直到第一个非空白字符来执行 (仍未读取),或者直到无法读取更多字符为止.这 指令永远不会失败.

§7.21.6.2/5 A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.

您的代码成为scanf(" %c%f%d",&b[i].name,&b[i].price,&b[i].pages);.

请注意,此处fflush不应使用.

Note that fflush should not be used here.

§7.21.5.2/2如果stream指向输出流或更新流,其中 没有输入最新的操作,fflush函数会导致 该流的所有未写入数据将被传递到主机 要写入文件的环境;否则,行为是 未定义.

§7.21.5.2/2 If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

这篇关于为什么scanf()不等待键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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