fflush(stdin)函数无效。

6

我似乎无法弄清楚这段代码出了什么问题:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>


#define MAX 100
#define TRUE 1
#define FALSE 0

char sect_cat;
char customer_name[MAX];
char customer_number[MAX];      /* error handling is easier */

int prev_unit = 0;
int current_unit = 0;
int consumed = 0;
int set = FALSE;

float init_bill;
float tax;
float total_bill;


    void get_userinfo()
    {

            printf("Enter sector category: ");
            scanf("%c", &sect_cat);
        printf("Enter customer name: ");
        fflush(stdin);
        scanf("%sn", &customer_name);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter customer number: ");
            fflush(stdin);
            scanf("%s", customer_number);

            int i;
            int error;
            for (i=0, error=0; i<strlen(customer_number); i++)
            {
                if (isdigit(customer_number[i]))
                {
                }
                else
                {
                    error = 1;
                }
            }
            if (error == 0)
            {
                set = TRUE;
            }
            else
                printf("ERROR: Only numbers are allowed\n");
        }

        printf("Enter previous unit: ");
        fflush(stdin);
        scanf("%d", &prev_unit);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter current unit: ");
            fflush(stdin);
            scanf("%d", &current_unit);

            if (prev_unit > current_unit)
            {
                printf("ERROR: Current unit must be larger than previous unit\n");
            }
            else
                set = TRUE;
        }
        consumed = current_unit - prev_unit;
    }



int main()
{


/* Introduce program to users */

        printf("\nThis program computes your electric bill based on these sector categories\n\n");

    printf("\tResidential(R)\n");
    printf("\tIndustrial(I)\n");
    printf("\tCommercial(C)\n\n");

    printf("Press any key to continue...");
    fflush(stdin);
    getchar();  

应用templatetypedef的解决方案后,程序现在等待用户输入customer_name。然而,输入包含空格的字符串会导致错误,并且程序假设空格后面的单词是下一个提示的输入。
Enter sector category: r
Enter customer name: George of the Jungle
Enter customer number: ERROR: Only numbers are allowed
Enter customer number: ERROR: Only numbers are allowed
Enter customer number:

8
fflush(stdin) 是未定义行为。 - Mysticial
4
将来,与其倾泻一个没有行号或相关信息指示的庞大程序,不如尝试发布一个尽可能少包含无关代码的代码示例,以便我们更清楚地帮助您解决问题。(此外,您甚至可能会自己解决它!) - Chris Lutz
3个回答

10
fflush函数不能刷新输入流中的数据。它的作用是将输出流中缓冲的数据推送到目的地。这在这里有记录。正如在这个早期的 SO 问题中所示,尝试使用fflush(stdin)会导致未定义的行为,因此最好避免使用它。
如果您想吃掉在用户完成输入字符时输入的换行符,可以考虑以下方法:
scanf("%c%*c", &sect_cat);
这将会吞掉换行符,而不是留在stdin中。

希望能对您有所帮助!


1
我宁愿让楼主考虑使用fgets而不是scanf,但任何试图fflush(stdin)的人还有很长的路要走,所以我会妥协。 - Chris Lutz
3
预期的结果并没有实现。输入一个字母后,程序会等待另一个输入,然后才继续执行。 - Karen Langres Bague
虽然在ISO C标准中 fflush(stdin)(或任何读取流)是未定义的,但POSIX在文件可搜索的情况下定义了其行为。 不过,对于不可搜索的流仍然是未定义的,而stdin通常是不可搜索的(例如,如果它是终端或管道)。 - R.. GitHub STOP HELPING ICE
@KarenLangresBague 这是因为 scanf 的格式字符串中的空格字符指示 scanf 扫描 任意数量 的空格字符(包括零个),直到第一个非空格字符 - Spikatrix

0

我认为你想要写的是fflush(stdout)而不是fflush(stdin)


2
我认为这不是原帖作者的本意。我认为原帖作者打算使用fflush(stdin)清除stdin中的换行符,以便后续读取操作不会受到影响。 - templatetypedef

-1

fflush 应该与输出流一起使用,请参见文档此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接