虽然环忽略scanf函数的第二次 [英] While-loop ignores scanf the second time

查看:140
本文介绍了虽然环忽略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);
       ^

这篇关于虽然环忽略scanf函数的第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆