【问题标题】:How do I flush the i/o stream? I'm using scanf(), fflush(stdin) doesnt work如何刷新 i/o 流?我正在使用 scanf(),fflush(stdin) 不起作用
【发布时间】:2012-09-19 07:50:04
【问题描述】:

如何在不将换行符放入 scanf() 的情况下刷新输入缓冲区?因为我的教授不喜欢它。我试过 fflush();但它没有用。

#include <stdio.h>
#include <conio.h>
int CountUpper(char S[],int n)
{
    int i,cntr = 0;
    for(i = 0; i < n; i++)
        if(S[i] >= 'A' && S[i] <= 'Z')
            ++cntr;
    return cntr;
}
int main(void)
{
    int n,i;
    printf("Enter n: ");
    scanf("%d",&n);
    char array[n];
    for(i = 0; i < n; i++)
    {
        scanf("%c",&array[i]);
        //fflush(stdin);
    }
    printf("Number of uppercase characters in array: %d\n",CountUpper(array,n));
    getch();
    return 0;
}

【问题讨论】:

标签: c input stream scanf


【解决方案1】:

fflush 仅为输出流定义,fflush(stdin) 调用未定义的行为

您可以查看此以丢弃缓冲区中的输入:what can I use to flush input?

【讨论】:

  • 所以没有办法刷新scanf()?我不得不求助于 getchar()?
  • 是的,你可以getchar()来吃一个char。
猜你喜欢
  • 2015-06-26
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 2014-04-09
  • 2012-12-27
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多