getchar 在 switch case (c) 中不起作用 [英] getchar not working in switch case (c)

查看:47
本文介绍了getchar 在 switch case (c) 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using a very simple calculator program that prompts a user for an operation to perform, followed by a prompt for two integers on which to perform this operation. The program is supposed to loop after these operations, except in the case where the user enters the character 'q', at which point the program is supposed to quit.

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

单次计算正常工作,但随后执行奇怪;特别是它打印

Works correctly for a single calculation, but subsequently performs oddly; in particular it prints

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

总之.

清除输入缓冲区的标准方法 while (getchar() != '\n'); 没有解决这个问题.在此文本显示不正确的情况下,有二分之一的用户仍然可以使用程序,就好像指令按预期显示一样(因此用户可以键入诸如 +、回车、然后是一些整数和回车等操作,并且程序将从那时起正确执行)但是,每隔一段时间,无论输入如何,程序都会显示按下无效键.按 q 退出".

The standard method of clearing the input buffer while (getchar() != '\n'); doesn't fix this. One out of two times that this text displays incorrectly the user can still use the program as if the instructions were displaying as they should (so the user can type an operation such as +, carriage return, and then some integer and a carriage return, and the program will perform correctly from that point on) Every other time however the program will put "Invalid key pressed. Press q to exit" regardless of input.

推荐答案

这里其他人所说的都是真的,getchar() 返回一个 int 但这不是你的问题.

What everyone else here is saying is true, getchar() returns an int but that's not your problem.

问题在于 getchar() 在您使用它后会留下一个换行符.如果您打算使用 getchar(),您必须总是之后使用换行符.这个简单的修复:

The problem is that getchar() leaves a newline character after you use it. If you're going to use getchar() you must always consume the newline char afterwards. This simple fix:

   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]);

这将消除问题.每次你输入 <somechar><enter> 它实际上是在缓冲区中放置两个字符,例如,如果我点击 + 并输入我得到:

and that will eliminate the problem. Every time you type <somechar><enter> it's really putting two characters on the buffer, for example if I hit + and enter I'm getting:

'+''\n'  // [+][\n]

getchar() 只会获取其中的第一个,然后当 getchar() 再次被调用时,它不会等待您的输入,它只会获取 '\n' 并移至 scanf()

getchar() will only get the first of these, then when getchar() is called again it won't wait for your input it will just take that '\n' and move on to the scanf()

这篇关于getchar 在 switch case (c) 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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