C++ C++;计算器不像我想象的那样工作
为什么我的计算器不能正常工作?Visual Studio只是问我是否要使用上一次成功构建 我看不出用户的输入是否计算为字符串吗 这是我的代码:C++ C++;计算器不像我想象的那样工作,c++,operators,calculator,C++,Operators,Calculator,为什么我的计算器不能正常工作?Visual Studio只是问我是否要使用上一次成功构建 我看不出用户的输入是否计算为字符串吗 这是我的代码: #include <iostream> using namespace std; int main() { int operation; int a; int b; cout << "Choose an operation:\n"; cout << "+ = Additi
#include <iostream>
using namespace std;
int main() {
int operation;
int a;
int b;
cout << "Choose an operation:\n";
cout << "+ = Addition\n";
cout << "- = Subtraction\n";
cout << "* = Multiplication\n";
cout << "/ = Division\n";
cin >> operation;
if (operation == "+") {
cout << "Enter a number! \n";
cin >> a;
cout << "Enter another number! \n";
cin >> b;
sum = a + b;
cout << sum;
}
if (operation == "-") {
cout << "Enter a number! \n";
cin >> a;
cout << "Enter another number! \n";
cin >> b;
sum = a - b;
cout << sum;
}
if (operation == "*") {
cout << "Enter a number! \n";
cin >> a;
cout << "Enter another number! \n";
cin >> b;
sum = a * b;
cout << sum;
}
if (operation == "/") {
cout << "Enter a number! \n";
cin >> a;
cout << "Enter another number! \n";
cin >> b;
sum = a / b;
cout << sum;
}
}
#包括
使用名称空间std;
int main(){
int操作;
INTA;
int b;
cout b;
总和=a-b;
库塔;
cout>b;
总和=a*b;
库塔;
cout>b;
总和=a/b;
您可以将操作声明为std::string
,这样比较就可以了,也可以将其声明为char
char operation;
并进行比较
if (operation == '+'){...}
请详细说明出了什么问题!(接近投票…)操作
声明为int
。假设我重新考虑sum
变量的命名…@Alanay“为什么会这样?”再次彻底阅读您的代码!您正在比较char*
指针值,而不是它们的内容。谢谢,我刚刚意识到我需要将sum声明为int;P