如何解决如果没有在开始时询问程序,则跳过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 举报,一经查实,本站将立刻删除。