C getchar()不等待输入/条件循环不int [英] C getchar() doesn't wait for input/ conditional loop doesn't int

查看:231
本文介绍了C getchar()不等待输入/条件循环不int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的C控制台应用程序计算器添加一项功能,该计算器会提示用户决定是否要使用以下方法执行其他计算: y n ,但在测试中, getchar()拒绝等待输入,程序继续进行,就好像它已收到有效输入一样。以下是该功能的最小示例:

I am trying to add a feature to my C console application calculator that prompts the user to decide whether they want to perform another calculation using: y or n, but in testing, getchar() refuses to wait for input and the program proceeds as though it has received valid input. The following is a minimal example of the feature:

main()
{
    char newCalculation;

    do{
        lengthFormula(); /* main calculation formula */
        printf("Would you like to do another calculation? (Y/N)");
        newCalculation = getchar();

    }while(tolower( newCalculation ) == 'y');

    if(tolower(newCalculation) == 'n'){
        exitProgram(); /* exit the program */
    }

    while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y'){
        printf("This is not a valid response.\n Please enter \"Y\" 
                if you want to do another calculation, 
                or enter \"N\" to exit.\n");
        newCalculation = getchar();
    }
    return 0;
}

当我运行此程序时,程序不会等待输入后:

When I run this, the program does not wait for input after:

您想再做一次计算吗? (是/否)

,而是继续进行,就好像收到了无效的输入一样。结果是它一个接一个地吐出提示和无效的输入通知而没有空格:

, but instead proceeds as though it has received invalid input. The result is that it spits out the prompt and the invalid input notice one after the other without a space:

你想再做一次计算吗? ? (是/否)

这不是有效回复。

如果您想进行其他计算,请输入\Y \,或输入\N \退出。

如果我在此之后输入y main( )返回 0 ,程序终止。

If I enter a "y" after this, main() returns 0 and the program terminates.

是否有人能够看到我的位置这里出了问题?
为什么控制台不会等待 getchar()的输入?
为什么有效输入会在第一次无效回复后终止程序?

Is someone able to see where I went wrong here? Why won't the console wait for input at getchar()? Why does valid input terminate the program after the first invalid response?

PS:请不要告诉我读书或叫我离开丹尼斯·里奇(Dennis Ritchie)或之前关于投入的SO讨论之一。我一直在研究Richie对I / O的讨论,以及来自Lynda.com和Wiley的类似文本,据我所知,之前的它不会等待输入的帖子都没有解决我的问题。

P.S.: Please don't tell me to "read a book" or shoo me away to Dennis Ritchie or one of the previous SO discussions on input. I've been poring over Richie's discussion of I/O, as well as similar texts from Lynda.com and Wiley, and none of the previous "it won't wait for input" posts addresses my issue as far as I can tell.

@simplicisveritatis这是我尝试过的代码修改。仍然有相同的getchar问题。

@simplicisveritatis Here is the modification of your code that I tried. Still have the same getchar issues.

int main(void)
{
    /* local variable declaration */
    char newCalculation = 'y';

    /* main function */
    /*if(tolower( newCalculation ) == 'y')
    {
        lengthFormula(newCalculation);
    }*/
    do
    {
        lengthFormula();

        printf("Would you like to do another calculation? (Y/N)");
        newCalculation = getchar();

        if( tolower( newCalculation ) == 'n' )
        {
            exitProgram();
        }

        while( tolower( newCalculation ) != 'n' && tolower( newCalculation ) != 'y' )
        {
            printf("This is not a valid response.\n Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.\n");
            newCalculation = getchar();
        }
    }while( tolower( newCalculation ) == 'y' );
    return 0;
}


推荐答案


为什么控制台不会等待getchar()的输入?

Why won't the console wait for input at getchar()?

最有可能的是,你的函数 lengthFormula ()读取输入(例如,通过使用 scanf()或其他),但不读取行结束字符来自输入缓冲区的\ n 。然后从 lengthFormula()返回后, getchar()必须从输入缓冲区读取剩余内容而不是请求新输入。

Most probably, your function lengthFormula() reads input (e. g. by using scanf() or whatever), but doesn't read the line ending character \n from the input buffer. Then after returning from lengthFormula(), the getchar() has to read remaining content from the input buffer rather than requesting fresh input.


为什么有效输入会在第一个无效的
响应后终止程序?

Why does valid input terminate the program after the first invalid response?

那是因为你的

    while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y')

之后也是如此在 n 的响应之后, y 的响应 - 它离开循环并转到以下

does the same after a response of y as after a response of n - it leaves the loop and gets to the following

    return 0;

这篇关于C getchar()不等待输入/条件循环不int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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