While-loop 第二次忽略 scanf [英] While-loop ignores scanf the second time
本文介绍了While-loop 第二次忽略 scanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
int main ()
{
char loop='y';
while(loop != 'n') {
printf("loop? ");
scanf("%c", &loop);
if(loop != 'y') {
loop='n';
}
}
return 0;
}
如果我输入y",他会重新启动 while 循环,但第二次忽略 scanf 并在此之后结束循环.有人可以帮忙吗?
If I type in 'y' he restart the while-loop but ignores the scanf the second time and end the loop after that. Can anyone help?
推荐答案
确保 scanf
丢弃换行符.改为:
Make sure the scanf
discards the newline. Change it to:
scanf(" %c", &loop);
^
这篇关于While-loop 第二次忽略 scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文