c - 在 C 中发出清除输入流

标签 c buffer fflush

我最近开始学习 C,在尝试制作一个简单的计算器时,我在尝试清除输入流时遇到了一个问题。我尝试使用 fflush(stdin); 但显然,它没有做任何事情。

Operating System: macOS Sierra (Version 10.12.5 (16F73))

Apple LLVM version 8.1.0 (clang-802.0.42)

Editor Used: Sublime Text 3

这是我的代码:

#include <stdio.h>

int main()
{
    int num1, num2;
    char opr;
    char choice = 'y';

    while (choice == 'y') {
        printf("Enter numbers separated with a comma: \n");
        scanf("%d, %d", &num1, &num2);

        printf("Enter an operator: \n");
        fflush (stdin);
        scanf("%c", &opr);

        switch (opr) {
            case '+':
                printf("The sum is %ld\n", (long int) num1 + num2);
                break;
            case '-':
                printf("The difference is %ld\n", (long int) num1 - num2);
                break;
            case '*':
                printf("The product is %ld\n", (long int) num1 * num2);
                break;
            case '/':
                printf("The quotient is %ld and the remainder is %ld\n",
                    (long int) num1 / num2, (long int) num1 % num2);
                break;
            default:
                printf("You've entered an undefined operator\n");
                printf("Please enter + - * or /\n");
                break;
        }

        printf("Do you want to repeat? (y/n):  \n");
        fflush(stdin);
        scanf("%c", &choice);
    }

    return 0;
}

这是终端输出:

$ ./calculator_basic
Enter numbers separated with a comma:
20, 30
Enter an operator:
You've entered an undefined operator
Please enter + - * or /
Do you want to repeat? (y/n):
n

我发现一个简单的解决方案是简单地交换

printf("Enter numbers separated with a comma: \n");<br>
scanf("%d, %d", &num1, &num2);


printf("Enter an operator: \n");
fflush (stdin);
scanf("%c", &opr);

我知道问题是由于 enter 键在 scanf("%d, %d", &num1, &num2); 中输入数字后存储在输入缓冲区中所致。 scanf("%c", &opr); 然后使用这个存储的 enter 键(在缓冲区中)作为它的输入,这会产生问题(即:没有一种情况在我定义的 switch 中工作并且默认情况下运行,所有这一切甚至我都无法输入运算符)

我尝试在获取新字符输入之前使用 fflush(stdin);,但它似乎不起作用。

当我尝试在 Windows 系统中运行此代码时,fflush(stdin); 可以正常工作,但无论我使用何种编辑器,它都无法在 macOS 上运行(尝试了 Sublime Text 3 和 Visual Studio Code)

任何人都可以帮助我了解为什么 fflush(stdin); 可能无法正常工作。 我也在互联网上读到过它,一种解决方案是使用 cin.get(); 而不是 getchar();。我试过了,但效果不佳。我不想交换这两段代码(上面提到的),因为那会受到打击和审判。我想以相同的顺序运行代码(先输入数字,然后输入运算符)

此外,我知道之前有人问过有关 fflush(stdin) 的问题,但我已经阅读了答案,但似乎无法找出对我有用的任何内容。

最佳答案

fflush(stdin); 是未定义的行为。

Per the C Standard:

7.21.5.2 The fflush function

...

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.

关于c - 在 C 中发出清除输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525709/

相关文章:

c - 为什么 scanf_s 函数没有正确接受输入?

c - 如何在 python 中使用 struct 模块解压缩字节?

sql - SSIS - 值太大,无法放入缓冲区的列数据区

c# - C# 中是否有类似于 C 中的 fflush() 的东西?

c - fflush() 始终返回 0 但将 errno 设置为 11(资源暂时不可用)

c - Vala 和 Genie 生产准备好了吗?

c - 通过函数添加时,新节点未正确添加到链表末尾

node.js - 在nodejs中分割和重建文件会改变某些格式的长度

c++派生结构来管理不同的版本