【问题标题】:C getchar() doesn't wait for input/ conditional loop doesn't intC getchar() 不等待输入/条件循环不 int
【发布时间】:2015-09-19 18:22:00
【问题描述】:

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

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

当我运行这个时,程序不会等待输入:

Would you like to do another calculation? (Y/N)

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

Would you like to do another calculation? (Y/N)

This is not a valid response.

Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.

如果我在这之后输入"y"main() 返回0 并且程序终止。

有人能看到我在哪里出错了吗? 为什么控制台不等待getchar() 的输入? 为什么有效输入在第一次无效响应后终止程序?

P.S.:请不要告诉我“读一本书”或把我赶到 Dennis Ritchie 或之前关于输入的 SO 讨论之一。我一直在仔细研究 Richie 对 I/O 的讨论,以及来自 Lynda.com 和 Wiley 的类似文本,据我所知,之前的“它不会等待输入”的帖子都没有解决我的问题。

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

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

【问题讨论】:

  • 在完成@user3121023 提供的更正后,程序在重新输入错误条目后退出,因为没有循环让它重新开始。如果您正确缩进/对齐代码,这将是显而易见的。

标签: c loops io console


【解决方案1】:

你的代码有很多问题: 主要应该是:

int main(void){return 0;}

你需要转换 getchar(阅读 getchar)并且应该是:

newCalculation = (char)getchar();

您对 do{}while 的处理方式; + while{} 也用错了。

尝试以下方法:

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

int main(void){
    int     validate;
    char    menu_choice;
    validate = 0;

    do{
        printf("Would you like another go?(y/n):\t" );

        if(scanf(" %c", &menu_choice ) == 1){
            if((menu_choice=='y') || (menu_choice=='Y')){
                printf("You choosed Yes\n\n\n");
                validate = 1;
            }else if((menu_choice=='n') || (menu_choice=='N')){
                printf("You choosed No\n\n\n");
                validate = 2;
            }else{
                printf("Wrong Input.\n\n\n");
                validate = 0;
            }
        }
    }while( validate == 0 || validate == 1);

    printf("Goodbye\n");
    return 0;
}

【讨论】:

  • “你需要转换 getchar”是有问题的建议。使用getchar() 时,返回值应保存为int,以便代码可以识别256 个字符中的1 个或EOF
【解决方案2】:

while 循环中包含您的三种情况:退出条件输入错误计算

main(){
do{
    printf("Would you like to do another calculation? (Y/N)");
    // get input
    char newCalculation;
    newCalculation = getchar();

    // exit condition
    if(tolower(newCalculation) == 'n'){
        exitProgram(); /* exit the program */
    }
    // wrong input condition
    else if(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");
       // you should clear the input stream from the wrong input
    } 
    else{
        // calculate  
        lengthFormula();  
     }
}while(tolower(newCalculation) == 'y');

return 0;
}

【讨论】:

  • 当我运行那个版本的 'main()' 时,第一个输入提示是'“你想再做一次计算吗?(Y/N)”',然后当我输入一个 'y ', 'main()' 返回 '0' 并终止。我希望程序在启动时调用一次“lengthFormula()”,然后再打印“另一个计算?”提示,我想输入 'y' 再次调用 'lengthFormula()。检查上面我尝试了什么。我遇到了和最初一样的问题。
  • @Nathaniel Lindsey 在没有输入值的情况下调用lengthFormula() 毫无意义。你的lengthFormula()返回什么?
【解决方案3】:

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

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

为什么有效输入在第一次无效后终止程序 回应?

那是因为你的

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

y 的响应之后与n 的响应之后执行相同的操作 - 它离开循环并进入以下内容

    return 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2016-07-22
    • 2016-07-19
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多