空调回路串打印两次? (使用scanf函数(QUOT;%C")) [英] C loop prints string twice? (using scanf("%c"))

查看:182
本文介绍了空调回路串打印两次? (使用scanf函数(QUOT;%C"))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我可能愚蠢的问题,但我想练循环了一下,这个就出来了idea.Basically它要求你在一个循环进入与否,当你在,就问你点事做。问题是,就在我进入循环将它传递给scanf函数之一,并等待输入之前打印printf的字符串两次。我不明白。
所有帮助是值得欢迎的!这里的code:

 的#include<&stdio.h中GT;诠释的main()
{
    烧焦检查='A';
    烧焦int_check ='A';
    INT B = 0;
    的printf(想在去[Y或N] \\ n吗?);
    scanf函数(%C,&安培;检查);
    如果(检查=='Y'){
        而(1){
            的printf(等待:\\ n);
            scanf函数(%C,&安培; int_check);
            如果(int_check =='Q'){
                的printf(你出去,再见\\ n!);
                打破;
            };
        };
    }否则如果(检查=='N'){
        的printf(你不是,再见\\ n!);
    }其他{
        的printf(请输入'Y'或'N'\\ n);
    };
    返回0;
}


解决方案

如果您输入到终端如下:

  X

第一循环将看到 X

第二个循环将会看到一个换行符。

要解决这个最简单的方法是使用sscanf和函数getline。

Sorry for the probably dumb question, but i wanted to practice loops a bit and came out with this idea.Basically it ask you to enter or not in a loop and when you're in, it ask you for something to do.The problem is that just after i enter the loop it prints two times the printf string before passing to the scanf one and waiting for an input. I can't figure it out. All help is welcome! Here's the code:

#include <stdio.h>

int main() 
{
    char check = 'a';
    char int_check = 'a';
    int b = 0;
    printf("want to go in? [y or n]\n");
    scanf("%c",&check);
    if ( check == 'y') {
        while (1){
            printf("Waiting: \n");
            scanf("%c",&int_check);
            if ( int_check == 'q'){
                printf("You're out, bye!\n");
                break;
            };
        };
    } else if ( check == 'n'){
        printf("You're not in, see ya!\n");
    }else {
        printf("Please, type 'y' or 'n'\n");
    };
    return 0;
}

解决方案

If you input onto a terminal the following:

x

The first loop will see an x

The second loop will see a newline character.

The easiest way to work around this is to use sscanf and getline.

这篇关于空调回路串打印两次? (使用scanf函数(QUOT;%C&QUOT;))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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