getchar在开关情况下不工作(c)

getchar在开关情况下不工作(c),c,switch-statement,getchar,input-buffer,C,Switch Statement,Getchar,Input Buffer,使用一个非常简单的计算器程序,提示用户执行一个操作,然后提示输入两个要执行此操作的整数。程序应该在这些操作后循环,除非用户输入字符“q”,此时程序应该退出 #include <stdio.h> int main (void) { char c; int number[2], num1, num2, result; double num1d, num2d, resultd; int done=1;

使用一个非常简单的计算器程序,提示用户执行一个操作,然后提示输入两个要执行此操作的整数。程序应该在这些操作后循环,除非用户输入字符“q”,此时程序应该退出

#include <stdio.h>

int main (void)
    {
        char c;
        int number[2], num1, num2, result;
        double num1d, num2d, resultd;
        int done=1;

        while(done)
        {
        printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
        c = getchar();
        printf("\tplease enter a number \n");
        scanf("%d",&number[0]);
        printf("\tplease enter another number \n");
        scanf("%d",&number[1]);
        num1 = number[0];
        num2 = number[1];

        switch(c)
            {
            case('-'):
            result = num1-num2;
            printf("\nThe first number you entered subtracted by the second number is %d.\n", result);
            break;

            case('+'):
            result = num1+num2;
            printf("The first number you entered added to the second number is %d.\n", result);
            break;

            case('*'):
            result = num1*num2;
            printf("The first number you entered multiplied with the second number is %d.\n", result);
            break;

            case('/'):
            num1d = (double) num1;
            num2d = (double) num2;
            resultd = num1d/num2d;
            printf("The first number you entered divided by the second number is %g.\n", resultd);;
            break;

            case('q'):
            printf(" Now Exiting...\n");
            done=0;
            break;

            default:
            puts("Invalid key pressed. Press q to exit");
            break;
            }
        }

        return 0;
    }
printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
总共

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

getchar!='时清除输入缓冲区的标准方法\n′;不能解决这个问题。此文本显示不正确的两次中有一次,用户仍然可以使用程序,就好像指令按应有的方式显示一样,因此用户可以键入一个操作,例如+、回车,然后键入某个整数和回车,并且每隔一次,程序将从该点正确执行,但程序将按下无效键。无论输入如何,按q退出

您不应该将字符与更高级的输入函数(如scanf)混合使用。最好也使用scanf输入命令字符,但当然,在命令之后必须按enter键。我相信这是你问题的根源

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
另一方面,请注意getchar,不管它的名称如何,都返回int,而不是char。这是因为它可以返回EOF,EOF是一个特殊常量,其值不同于所有字符的值

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
此外,您应该始终检查I/O函数(如scanf)的返回值,如果输入与模式字符串不匹配,它们可能会失败

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

作为调试提示,您当然可以在解释c之前打印c的值,以便更容易地查看和理解程序的流程。

您不应该将字符与更高级的输入函数(如scanf)混用。最好也使用scanf输入命令字符,但当然,在命令之后必须按enter键。我相信这是你问题的根源

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
另一方面,请注意getchar,不管它的名称如何,都返回int,而不是char。这是因为它可以返回EOF,EOF是一个特殊常量,其值不同于所有字符的值

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
此外,您应该始终检查I/O函数(如scanf)的返回值,如果输入与模式字符串不匹配,它们可能会失败

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

作为调试提示,您当然可以在解释c之前打印c的值,以便更容易地查看和理解程序的流程。

您的getchar应该返回int。原因如下

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
getchar reads characters from the program's standard input 
and returns an int value suitable for storing into a char. 
The int value is for one reason only: not only does getchar 
return all possible character values, but it also returns an 
extra value to indicate that end-of-input has been seen. 
The range of a char might not be enough to hold this extra value, 
so the int has to be used.

因此,基本上您需要在代码中将char c更改为int c,getchar应该返回int

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
getchar reads characters from the program's standard input 
and returns an int value suitable for storing into a char. 
The int value is for one reason only: not only does getchar 
return all possible character values, but it also returns an 
extra value to indicate that end-of-input has been seen. 
The range of a char might not be enough to hold this extra value, 
so the int has to be used.

所以基本上你需要在你的代码中将char c改为int c,我猜第一次可以,但下一次不行。这是因为scanf调用将换行符留在输入缓冲区中,因此下次在循环中调用getchar时,它将返回换行符。在scanf调用中的格式后添加空格

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
它将丢弃缓冲区中剩余的空白

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

使用调试器逐步完成代码并检查要验证的变量。

我猜它第一次起作用,但下次不起作用。这是因为scanf调用将换行符留在输入缓冲区中,因此下次在循环中调用getchar时,它将返回换行符。在scanf调用中的格式后添加空格

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
它将丢弃缓冲区中剩余的空白

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

使用调试器逐步完成代码并检查变量以进行验证。

这里的其他人都说的是真的,getchar返回int,但这不是您的问题

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
问题是getchar在使用后会留下一个换行符。如果要使用getchar,则必须始终在之后使用换行符。这是一个简单的解决方案:

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
   printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
    c = getchar();
    getchar();     //<-- here we do an extra getchar for the \n
    printf("\tplease enter a number \n");
    scanf("%d",&number[0]);
    printf("\tplease enter another number \n");
    scanf("%d",&number[1]);

getchar将只获取其中的第一个,然后当再次调用getchar时,它不会等待您的输入,它将只获取“\n”并继续扫描。这里的其他人都说是真的,getchar返回一个int,但这不是您的问题

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
问题是getchar在使用后会留下一个换行符。如果要使用getchar,则必须始终在之后使用换行符。这是一个简单的解决方案:

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");
   printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
    c = getchar();
    getchar();     //<-- here we do an extra getchar for the \n
    printf("\tplease enter a number \n");
    scanf("%d",&number[0]);
    printf("\tplease enter another number \n");
    scanf("%d",&number[1]);

getchar将只获取其中的第一个,然后当再次调用getchar时,它将不会等待您的输入,它将只获取“\n”并转到scanf

我听说getchar返回整数这一事实,这让我非常惊讶。然而,你知道为什么这可以解释这种奇怪的行为吗?@Duncan-不知道。从getchar中获取一个char的事实只意味着输入可能会溢出,即int可以容纳多个char。你的问题是保留换行符,看看我的答案。我听说getchar返回整数,这让我很惊讶。然而,你知道为什么这可以解释这种奇怪的行为吗?@Duncan-不知道。这个
printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

从getchar中获取一个char的事实只意味着输入可能会溢出,即int可以容纳多个char。您的问题是保留换行符请参阅我的答案。虽然getchar返回int是真的,但该建议并不能解决问题。虽然getchar返回int是真的,但该建议并不能解决问题。谢谢兄弟,这很有用:谢谢兄弟,这很有用: