【问题标题】:Simple Calculator in C flow of input skipping over scanf() [duplicate]跳过scanf()的C输入流中的简单计算器[重复]
【发布时间】:2013-09-21 23:52:57
【问题描述】:

我尝试编写我的第一个计算器,并在网上找到了一些示例,然后我对其进行了更改以使它们在流程方面更容易。但是,当我从这里更改流程时:

#include <stdio.h>

main()
{
    char operator;
    float num1,num2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c" ,&operator);
    printf("Enter first operand: ");
    scanf("%f" ,&num1);
    printf("Enter second operand: ");
    scanf("%f" ,&num2); 

    switch(operator)
    {
        case '+':
            printf("num1+num2=%.2f\n" ,num1+num2);
            break;
        case '-':
            printf("num1-num2=%.2f\n" ,num1-num2);
            break;
        case '*':
            printf("num1*num2=%.2f\n" ,num1*num2);
            break;
        case '/':
            printf("num1/num2=%.2f\n" ,num1/num2);
            break;
        default: //of operator is other than +, -, *, /, erros message shown
        printf("Error! Invalid operator, this is basic math only.\n");
    }       
    return 0;
}

到这里:

#include <stdio.h>

main()
{
    char operator;
    float num1,num2;


    printf("Enter first operand: ");
    scanf("%f" ,&num1);
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c" ,&operator);
    printf("Enter second operand: ");
    scanf("%f" ,&num2); 

    switch(operator)
    {
        case '+':
            printf("num1+num2=%.2f\n" ,num1+num2);
            break;
        case '-':
            printf("num1-num2=%.2f\n" ,num1-num2);
            break;
        case '*':
            printf("num1*num2=%.2f\n" ,num1*num2);
            break;
        case '/':
            printf("num1/num2=%.2f\n" ,num1/num2);
            break;
        default: //of operator is other than +, -, *, /, erros message shown
        printf("Error! Invalid operator, this is basic math only.\n");
    }       
    return 0;
}

基本上改变了流程:输入运算符,然后输入第一个数字,然后输入第二个数字。 To:输入第一个数字,然后输入运算符,然后输入第二个数字。 我的问题是,当我这样做时,我看到了 Enter 运算符,但程序跳过了输入运算符的选项并要求:输入第一个数字,然后输入第二个数字。响应是默认开关。

【问题讨论】:

  • 你看过this的问题吗?

标签: c flow


【解决方案1】:

这是因为当您输入第一个 scanf 的输入时,换行符保留在缓冲区中,所以下面的 scanf 将其作为输入接收,只需在每个 scanf() 之后放置一个 getchar() 即可解决它

【讨论】:

    【解决方案2】:

    输入缓冲区中的换行符。

    使用scanf("%f",... 时,%f 使用前导空格,但数字后没有尾随空格 - 通常是 \n

    当使用scanf("%c",... 时,%c 不会使用前导空格,char 之后也不会使用尾随空格。

    要消耗剩余的空白(例如,前一个 scanf() 中的 \n),只需在 %c 前面加上一个空格。

    // scanf("%c" ,&operator);
    scanf(" %c" ,&operator);  // add space.
    

    【讨论】:

    • 你说得对,这行得通。这种现象的原因是什么?这是 c 语言中的一个老错误,还是有目的且有原因的?
    • 假设“这是一个旧错误”中的“this”是指%c 消耗空格:这不是错误。 %c 扫描 1 个 charany 1 个char,无一例外,包括' ''\n' 等,甚至'\0'(在阅读文本时很少遇到。)为避免扫描空白,预先添加 ' ' 会占用 %c 之前的所有空白。因此" %c" 不会存储空格。 %c 允许使用scanf() 扫描到单个char 一个空格字符。
    【解决方案3】:

    scanf 读取第一个运算符后输入的换行符正在被第二个 scanf 调用占用。更详细的解释见this问题。

    简而言之,写一个这样的函数,在每次scanf调用之后调用它。

    void clear_stdin(void)
    {
        while(getchar() != '\n');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2013-01-07
      • 1970-01-01
      • 2012-04-29
      相关资源
      最近更新 更多