如果没有在开始时询问程序,则跳过scanf“%c”,运算符

问题描述

每当我运行此代码(C)时,程序就会跳过 scanf(“%c”,运算符)。我只是在学习C,并且感到困惑。当我在请求任何其他输入之前要求scanf(“%c”,operator)时,程序正在按预期方式工作。这里在玩什么?

#include <stdio.h>
#include <string.h>
int main() {
    
    char variable[2];
    char operator;
    int first,second;
    
    printf("Enter the variable: \n");
    scanf("%s",&variable);

    printf("Enter an operator (+,-): ");
    scanf("%c",&operator);

    printf("Enter two operands (one before equal and one after equal sign): ");
    scanf("%d %d",&first,&second);

    switch (operator) {
    case '+':
        printf("x = %d and x = %d",first + second,first - second);
        break;
    case '-':
        printf("x = %d and x = %d",first - second);
        break;
        // Absolute Value for simple calculations
    default:
        printf("Error! Difficult computation");
    }

    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)