我从一本书中复制代码,这是一个像打击一样的循环,
for(;;)
{
printf("enter a value");
scanf("%lf",&value);
tot al+=value;
++count;
printf("do you want to enter another value?(N or Y):");
scanf("%c",&answer);
if(tolower(answer)=='n')
break;
}
但它有一些奇怪的行为,当我评估它时,它给出了输出
[tintin@tintin-laptop Documents]$ ./test
this enter a value3
do you want to enter another value?(N or Y):enter a value
我仔细检查了最后当我改变时
scanf("%c",&answer);
在 %c 之前有一个空格,即
scanf(" %c",&answer);
它表现正常
[tintin@tintin-laptop Documents]$ ./test
this enter a value2
do you want to enter another value?(N or Y):y
enter a value3
do you want to enter another value?(N or Y):
为什么会发生这种事情?