即使使用fflush [duplicate]后,第二scanf也不起作用>> [

问题描述 投票:1回答:1

#include <stdio.h> int main(){ int inpt, pos = 0, neg = 0, zero = 0; char cont = 'y'; while(cont == 'y' || cont == 'Y') { printf("\nPlease enter a Number: "); scanf("%d", &inpt); if( inpt < 0) { neg++; } else if( inpt > 0) { pos++; } else { zero++; } fflush(stdin); printf("\n Do you want to Continue:"); scanf("%c", &cont); printf("\nvalue: %d", cont); } printf("\nTotal Number of Positive Integers: %d \n Total Number of Negative Integers: %d \n Total Number of Zeros: %d ", pos, neg, zero); return 0; }

[幸运的是,我尝试调试并打印了cont的值,发现它的值为10(这是键盘中Enter键的ASCII值)。我希望fflush应该清除了输入缓冲区,但事实并非如此。 

关于如何修复的任何建议将不胜感激。

下面是一个我正在尝试运行的简单程序,但是很遗憾,它无法正常运行,当我第二次尝试输入时,它不会接受输入。 #include

int main(){...

c scanf
1个回答
0
投票
[尝试使用%*c放弃缓冲区剩余的\n字符。
© www.soinside.com 2019 - 2024. All rights reserved.