【问题标题】:Don't Know where i am going wrong while flushing the stdin buffer in below code [duplicate]在下面的代码中刷新标准输入缓冲区时不知道我哪里出错了[重复]
【发布时间】:2018-04-25 01:11:57
【问题描述】:

I have attached output image of my program. you can see the problem i am facing by clicking here

下面的代码是我的程序,它使用我自己版本的内置函数 isalpha() 和 isalnum( )。 不适用于 while 循环的第二次迭代

#include <stdio.h>
#define NUM     1
#define ALPHA   2
#define ASCII   3
#define BLANK   4


int main()
{
  char option;

  do
  {
      //declaration of function
      char character;
      int user_option, status;

      //get the character from the user
      printf("Enter the Character:");

      //clears both buffers to read the character
      fflush(stdin);
      fflush(stdout);


       //reads one character at a time
      character = getchar();

      //prompt the user for the option to check
      printf("Choice Below Option\n");
      printf("1.isalnum\n2.isalpha\n");
      printf("Enter your Option:");
      scanf("%d", &user_option);

      //validation of the user_option
      switch(user_option)
      {
          case 1:
                  status = my_isalnum(character);
                  if(status == NUM)
                  {
                      printf("Character '%c' is an number", character);
                  }
                  else
                  {
                      printf("Character '%c' is not a number", character);
                  }
                  break;

          case 2:
                  status = my_isalpha(character);
                  if(status == ALPHA)
                  {
                      printf("Character '%c' is an Alphabet", character);
                  }
                  else
                  {
                     printf("Character '%c' is not Alphabet",character);
                  }
                  break;
            default:
                 puts("Invalid Choice.....");
     }

    printf("\nDo you want to continue?[Y/N]:");
     scanf(" %c", &option);
 }while (option == 'Y' || option == 'y');
 fflush(stdin);
 fflush(stdout);
 return 0;
}
 //Function chaecks for the Number
 int my_isalnum(char character)
 {
   return (character >= '0' && character <= '9')?  NUM : -1;
  }

  //functionn checks for the alphabets
  int my_isalpha(char character)
  {
      return (character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z') ? ALPHA: -1;

   }

上面的代码第一次正常工作,但在 while 循环的第二次迭代中。当我将“Y”作为输入代码直接跳转到 scanf 的 user_option 部分时,然后等待“getchar()”- 函数。

即使使用 fflush() 函数填充缓冲区后,我也无法提示程序输入字符。

【问题讨论】:

    标签: c while-loop buffer stdin


    【解决方案1】:

    这里

    //clears both buffers to read the character
    fflush(stdin);
    

    fflush() 没有按预期清除 stdin 缓冲区。 fflush() 用于刷新输出流,如stdout,而不是输入流stdin

    要解决此问题,一种方法是使用额外的getchar() 来消耗\n 字符。例如

    scanf(" %c", &option);
    getchar(); /* dummy getchar */
    

    这里也有

    char character; /* change the type of character to int */
    character = getchar();
    

    getchar()返回类型是int而不是char,你需要改变character的类型。来自getchar()的手册页

    int getchar(void);
    

    【讨论】:

    • 其实多一个getchar也可能是致命的!你也必须检查它的价值。如果用户输入aa\n,则输入现在不同步。
    【解决方案2】:

    刷新stdin 是未定义的行为。刷新是针对输出流而不是输入流。

    按照标准§7.21.5.2

    如果流指向输出流或更新流,其中 没有输入最近的操作,fflush 函数会导致任何 该流的未写入数据将被传递到主机环境 写入文件;否则,行为未定义。

    同样int getchar(void) : get char() 返回一个int

    快速修复:从代码中删除 fflush(stdin)

    printf("\nDo you want to continue?[Y/N]:");
         scanf(" %c", &option);
         getchar(); // this dummy getchar() will consume the `\n` from stdin.
     }while (option == 'Y')
    

    注意:

    正如其他解决方案建议使用 fpurge() 是一个选项,但它又是非标准且不可移植的

    【讨论】:

    • 那么我如何刷新输入缓冲区以读取我的字符以进行第二次迭代@coderredoc
    • @MohmmedAsif.: 检查我的答案.. 把虚拟的 getchar 放在...它会工作
    • @MohmmedAsif.: 这个解决方案是可移植的..__fpurge 是不可移植的。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多