简体   繁体   English

getchar()函数未读取正确的值

[英]getchar() function not reading the correct value

When I enter 0 the program ends. 当我输入0时,程序结束。 But there is getchar() in if statement and it doesn't work, can you help me? 但是if语句中有getchar() ,它不起作用,您能帮我吗?

On case 0 I want it to get char from user. 在案例0中,我希望它从用户那里获取字符。 If 'N' or 'n' is entered the program will end but if not the program will start over again. 如果输入'N''n' ,程序将结束,但如果未输入,程序将重新开始。 (from sec1). (来自sec1)。

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

int main()
{
    int n;

    sec1:
    printf("Select a number between 4 and 0: ");
    scanf("%d[\n]", &n);

    switch(n)
    {
        case 0:
            puts("Are you sure?");
            puts("Yes(Y) or No(N)");
            if(getchar() == 'N') goto sec1;
            break;

        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;

        default:
            puts("Only numbers between 4 and 1 are accepted!");
            goto sec1;
            break;

    }
    system("pause");
    return 0;
}

working one 工作一个

int main()
{
    int n, c;

    sec1:
    printf("Select a number between 4 and 0: ");
    scanf("%d/n", &n);

    switch(n)
    {
        case 0:
            puts("Are you sure?");
            puts("Yes(Y) or No(N)");
            fflush(stdin);
            if (getchar() == 'N') goto sec1;
            break;

        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;

        default:
            puts("Only numbers between 4 and 1 are accepted!");
            goto sec1;
            break;

    }
    system("pause");
    return 0;
}

Change 更改

scanf("%d[\n]",&n);

to

scanf("%d\n",&n);

The reason that your original code does not work is because scanf wants to read an integer, a "[", a "\\n" and a "]". 您的原始代码不起作用的原因是scanf想要读取一个整数,即“ [”,“ \\ n”和“]”。 So when you input an integer followed by a "\\n", scanf only takes the integer(because it expects to see a "["). 因此,当您输入一个整数后跟一个“ \\ n”时,scanf仅采用该整数(因为它希望看到一个“ [”)。 Then the getchar will simply take the remaining "\\n". 然后,getchar将仅采用其余的“ \\ n”。 That's why your getchar() seems not working. 这就是为什么您的getchar()似乎不起作用的原因。

Hope it is helpful to you! 希望对您有帮助!

Probably you have some symbols in the input buffer when you are reading "N" or "n". 读取“ N”或“ n”时,输入缓冲区中可能有一些符号。 You need to flush an input buffer before new reading. 您需要在刷新新输入之前刷新输入缓冲区。 See this question for details. 有关详细信息,请参见此问题

Briefly just write: while (getchar() != '\\n') {} right after scanf(...) . 简而言之,只需编写: while (getchar() != '\\n') {}scanf(...)

I guess this is the terminal which is in cooked mode, thereby only feeding typed characters to the program when the user completes a line by hitting return. 我猜这是处于烹饪模式的终端,因此仅当用户通过按回车键完成一行时才将键入的字符提供给程序。 You can test it by feeding it input from a pipe instead of interactively from the terminal. 您可以通过从管道而不是从终端以交互方式输入输入来对其进行测试。

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

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