gpt4 book ai didi

c - 如何刷新 i/o 流?我正在使用 scanf(),fflush(stdin) 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:19 25 4
gpt4 key购买 nike

如何在不在 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;
}

最佳答案

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

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

关于c - 如何刷新 i/o 流?我正在使用 scanf(),fflush(stdin) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com