【问题标题】:fflush(stdin) function does not workfflush(stdin) 函数不起作用
【发布时间】:2012-02-25 17:03:33
【问题描述】:

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

#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:

【问题讨论】:

  • fflush(stdin) 是未定义的行为。
  • 在未来,与其转储一个庞大的程序(没有行号或相关信息在哪里的指示),不如尝试发布一个代码示例,以尽可能少的无关代码演示相同的问题,以便我们更清楚地帮助您。 (另外,你甚至可能最终自己解决它!)

标签: c stdin fflush


【解决方案1】:

我认为您的意思是写 fflush(stdout) 而不是 fflush(stdin)

【讨论】:

  • 我认为这不是 OP 的意思。我认为 OP 打算 fflush(stdin) 清除 stdin 中的换行符,以便以后的读取操作不会拾取它。
【解决方案2】:

fflush 函数不会从输入流中刷新数据;相反,它用于将缓冲在输出流中的数据推送到目的地。这记录在here。正如this earlier SO question 中所见,尝试使用fflush(stdin) 会导致未定义的行为,因此最好避免它。

如果您想在用户完成输入字符时从输入的返回字符中吃掉换行符,请考虑以下内容:

scanf("%c%*c", &sect_cat);

这将吃掉换行符,而不是将其留在stdin

希望这会有所帮助!

【讨论】:

  • 我宁愿 OP 考虑使用 fgets 而不是 scanf,但任何尝试使用 fflush(stdin) 的人还有一段路要走,所以我会解决的。
  • 它没有按预期工作。在输入字母之后,程序等待另一个输入然后继续。
  • 虽然fflush(stdin)(或在任何读取流上)未根据 ISO C 定义,但 POSIX 确实定义了文件可查找情况下的行为。不过,对于不可搜索的流,它仍然是未定义的,stdin 通常是不可搜索的(例如,如果它是终端或管道)。
  • @KarenLangresBague 这是因为scanf 格式字符串中的空白字符指示scanf 扫描任何个空白字符,包括无,直到第一个非空白字符.
【解决方案3】:

fflush 应该与输出流一起使用,请参阅文档here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 2015-06-26
    • 2023-04-04
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多