简体   繁体   English

C循环打印字符串两次? (使用scanf(“%c”))

[英]C loop prints string twice? (using scanf(“%c”))

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. 抱歉可能是愚蠢的问题,但我想练习循环并提出这个想法。基本上它要求你进入或不进入循环,当你进入时,它会要求你做点什么。问题就在我进入循环之后,它会打印两次printf字符串,然后传递给scanf并等待输入。 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 第一个循环将看到一个x

The second loop will see a newline character. 第二个循环将看到换行符。

The easiest way to work around this is to use sscanf and getline. 解决这个问题最简单的方法是使用sscanf和getline。

You can change the program to respond to keyboard immediately, ie without waiting for the user to press enter. 可以立即更改程序以响应键盘,即无需等待用户按Enter键。 It requires changing the properties of the input terminal, and is generally messier and less portable-seeming than line-oriented input. 它需要改变输入终端​​的属性,并且通常比面向行的输入更麻烦且更不便携。 This page describes how to do it, and here is the code modified to work that way: 此页面描述了如何执行此操作,以下是修改为以这种方式工作的代码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

struct termios saved_settings;

void
reset_term_mode(void)
{
  tcsetattr (STDIN_FILENO, TCSANOW, &saved_settings);
}

int main() 
{
    tcgetattr(STDIN_FILENO, &saved_settings);
    atexit(reset_term_mode);

    struct termios term_settings;

    tcgetattr(STDIN_FILENO, &term_settings);
    term_settings.c_lflag &= ~(ICANON|ECHO);
    term_settings.c_cc[VMIN] = 1;
    term_settings.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_settings);

    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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM