C:清除STDIN [英] C: Clearing STDIN

查看:177
本文介绍了C:清除STDIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上处于$ C $个cblocks每个printf的我之前的Windowsfflush(标准输入);它的工作原理。当我复制我的code到Linux,这是行不通的,也没有任何替代品的fflush(标准输入);我发现。无论我似乎做哪种方式,输入似乎并不在我的code中的缓冲区或东西要清除不正确。

 的#include<&stdio.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&limits.h中GT;
#包括LT&;&文件ctype.h GT;诠释的main()
{
   炭的pbuffer [10],ΣQ缓冲[10],kbuffer [10];
   INT p = 0时,Q = 0,K = 0;
   INT R,I,Q,计数,总和;
   烧焦一个[​​3];
   A [0] ='Y';
   而(A [0] =='Y'|| a [0] =='Y')
   {
      的printf(请输入p值:\\ n);
      与fgets(pbuffer的,的sizeof(的pbuffer),标准输入);
      P =与strtol(pbuffer的,(字符**)NULL,10);      的printf(请输入一个Q值:\\ n);
      与fgets(ΣQ缓冲,sizeof的(ΣQ缓冲),标准输入);
      Q = strtol将(ΣQ缓冲,(焦炭**)NULL,10);      的printf(请输入K值:\\ n);
      与fgets(kbuffer,sizeof的(kbuffer),标准输入);
      K = strtol将(kbuffer,(焦炭**)NULL,10);      而(P&所述; Q + 1)
      {
         Q =磷;
         总和= 0;
         计数= 0;
         而(Q&0)
         {
            算上++;
            R =答疑10;
            总和=总和+ POW(R,K);
            Q = Q / 10;
         }         如果(第==总和&放大器;&放大器;我→1&放大器;&放大器;计数== k)的
         {
            的printf(%d个\\ N,P);         }
         p ++;
         一个[0] ='Z';
      }
      而((一个[0] ='y'的)及!及(一个[0] =Y)及及(一个[0] ='N')及!及(一个[0 ]!='N'))
      {
         的printf(你想再次运行(Y / N)?);
         与fgets(一,sizeof的(a)中,标准输入);
      }
   }
   返回0;
}


解决方案

调用 fflush(标准输入)不标准,所以这种行为是未定义(见的this回答获得更多信息)。

而不是调用 fflush 标准输入,你可以调用 scanf函数,传递一个格式字符串指示功能读取一切直到并包括换行的'\\ n'字符,像这样的:

  scanf函数(%* [^ \\ n]的1%* [\\ N]);

中的星号告诉 scanf函数忽略的结果。

另一个问题是调用 scanf函数来读取字符到变量 A 与格式说明%S:当用户输入一个非空字符串,空终止造成缓冲区溢出,从而导致未定义行为(烧焦一个是一个字符的缓冲区;字符串Y有两个字符 - {'Y','\\ 0'} 与书面过去缓冲区的端第二个字符)。您应该更改 A 来有几个字符,并传递限制缓冲区 scanf函数

 的char a [2];
做{
    的printf(你想再次运行?(Y / N)\\ n)
    scanf的(%1,一个);
}而(A [0] ='Y'和;!&安培;!A [0] ='Y'和;&安培; A [0] ='N'和;!&安培;!A [0] ='N ');
}

basically in codeblocks for windows before each printf I have "fflush(stdin);" which works. When I copied my code to Linux, it doesn't work, nor does any of the alternatives for "fflush(stdin);" that I've found. No matter which way I seem to do it, the input doesn't seem to be clearing in the buffer or something in my code is incorrect.

#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <ctype.h>

int main()
{
   char pbuffer[10], qbuffer[10], kbuffer[10];
   int p=0, q=0, k=0;
   int r, i, Q, count, sum;
   char a[3];
   a[0]='y';
   while(a[0]=='y' || a[0]=='Y')
   {
      printf("Enter a p value: \n");
      fgets(pbuffer, sizeof(pbuffer), stdin);
      p = strtol(pbuffer, (char **)NULL, 10);

      printf("Enter a q value: \n");
      fgets(qbuffer, sizeof(qbuffer), stdin);
      q = strtol(qbuffer, (char **)NULL, 10);

      printf("Enter a k value: \n");
      fgets(kbuffer, sizeof(kbuffer), stdin);
      k = strtol(kbuffer, (char **)NULL, 10);

      while(p<q+1)
      {
         Q=p;
         sum=0;
         count=0;
         while(Q>0)
         {
            count++;
            r = Q%10;
            sum = sum + pow(r,k);
            Q = Q/10;
         }

         if ( p == sum && i>1 && count==k )
         {
            printf("%d\n",p);

         }
         p++;
         a[0]='z';
      }
      while((a[0]!='y') && (a[0]='Y') && (a[0]!='n') && (a[0]!='N'))
      {
         printf("Would you like to run again? (y/n) ");
         fgets(a, sizeof(a), stdin);
      }
   }
   return 0;
}

解决方案

Calling fflush(stdin) is not standard, so the behavior is undefined (see this answer for more information).

Rather than calling fflush on stdin, you could call scanf, passing a format string instructing the function to read everything up to and including the newline '\n' character, like this:

scanf("%*[^\n]%1*[\n]");

The asterisk tells scanf to ignore the result.

Another problem is calling scanf to read a character into variable a with the format specifier of " %s": when the user enters a non-empty string, null terminator creates buffer overrun, causing undefined behavior (char a is a buffer of one character; string "y" has two characters - {'y', '\0'}, with the second character written past the end of the buffer). You should change a to a buffer that has several characters, and pass that limit to scanf:

char a[2];
do {
    printf("Would you like to run again? (y/n) \n")
    scanf("%1s", a);
} while(a[0] !='y' && a[0] !='Y' && a[0]!='n' && a[0]!='N' );
}

这篇关于C:清除STDIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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