我正在尝试执行以下程序,用户输入数字直到他希望,但我的第二个scanf()被跳过。我尝试给scanf(“%[^ \ n] d”,& ans),但它不起作用。 Fflush(stdin)在netbeans中不起作用。请帮助并解释它为什么会发生。
int Ch5BG()
{
int number,positive,negative,zero;
char ans='y';
positive=negative=zero=0;
while(ans=='y'||ans=='Y')
{
printf("Please enter a number:\n");
scanf("%d",&number);
if(number>0)
positive++;
if(number<0)
negative++;
if(number==0)
zero++;
printf("Do you want to continue:");
scanf("%c",&ans);
}
printf("The number of positives entered are:%d",positive);
printf("The number of negatives entered are:%d",negative);
printf("The number of zeros entered are:%d",zero);
return 0;
}
输出如下:
Please enter a number:
3
Do you want to continue:The number of positives entered are:1The number of negatives entered are:0The number of zeros entered are:0
RUN SUCCESSFUL (total time: 1s)