C++ 计算器总是 returns 7208640
C++ Calculator always returns 7208640
这是我的计算器的代码:
#include <iostream>
using namespace std;
int main()
{
int fNumber, sNumber, sum;
string op;
cout << "You will be asked for two numbers and the operator to use on them.\nCurrently supported operators are:\n";
cout << " Addition: +\n Subtraction: -\n Multiplication: *\n Division: \\n Modulo: %\n\n";
cout << "First Number: ";
cin >> fNumber;
cout << "\nSecond Number: ";
cin >> sNumber;
cout << "\nOperator: ";
cin >> op;
if(op == "+"){
int sum = fNumber + sNumber;
} else if(op == "x" || op == "*"){
int sum = fNumber * sNumber;
} else if(op == "/"){
int sum = fNumber / sNumber;
} else if(op == "-"){
int sum = fNumber - sNumber;
} else if(op == "%"){
int sum = fNumber % sNumber;
} else{
cout << "\nPlease use a correct Operator\n";
return 1;
}
cout << "\n" << fNumber << " " << op << " " << sNumber << " = " << sum << "\n";
return 0;
}
我完全不知道为什么,但出于某种原因,无论我给它什么数字或运算符 returns“7208640”作为答案:
You will be asked for two numbers and the operator to use on them.
Currently supported operators are:
Addition: +
Subtraction: -
Multiplication: *
Division: \
Modulo: %
First Number: 6
Second Number: 2
Operator: /
6 / 2 = 7208640
Process returned 0 (0x0) execution time : 4.612 s Press any key to
continue.
if(op == "+"){
int sum = fNumber + sNumber;
应该是
if(op == "+"){
sum = fNumber + sNumber;
除第一个外,其他所有 int sum
都相同。你声明一个变量一次,而不是每次使用它。
同上,从 if 语句中删除 int 声明。
此外,请将求和变量的名称更改为类似"answer."减法、乘法、除法和模的结果分别称为差、积、商和余数。
这是我的计算器的代码:
#include <iostream>
using namespace std;
int main()
{
int fNumber, sNumber, sum;
string op;
cout << "You will be asked for two numbers and the operator to use on them.\nCurrently supported operators are:\n";
cout << " Addition: +\n Subtraction: -\n Multiplication: *\n Division: \\n Modulo: %\n\n";
cout << "First Number: ";
cin >> fNumber;
cout << "\nSecond Number: ";
cin >> sNumber;
cout << "\nOperator: ";
cin >> op;
if(op == "+"){
int sum = fNumber + sNumber;
} else if(op == "x" || op == "*"){
int sum = fNumber * sNumber;
} else if(op == "/"){
int sum = fNumber / sNumber;
} else if(op == "-"){
int sum = fNumber - sNumber;
} else if(op == "%"){
int sum = fNumber % sNumber;
} else{
cout << "\nPlease use a correct Operator\n";
return 1;
}
cout << "\n" << fNumber << " " << op << " " << sNumber << " = " << sum << "\n";
return 0;
}
我完全不知道为什么,但出于某种原因,无论我给它什么数字或运算符 returns“7208640”作为答案:
You will be asked for two numbers and the operator to use on them.
Currently supported operators are:
Addition: +
Subtraction: -
Multiplication: *
Division: \
Modulo: %
First Number: 6
Second Number: 2
Operator: /
6 / 2 = 7208640
Process returned 0 (0x0) execution time : 4.612 s Press any key to continue.
if(op == "+"){
int sum = fNumber + sNumber;
应该是
if(op == "+"){
sum = fNumber + sNumber;
除第一个外,其他所有 int sum
都相同。你声明一个变量一次,而不是每次使用它。
同上,从 if 语句中删除 int 声明。
此外,请将求和变量的名称更改为类似"answer."减法、乘法、除法和模的结果分别称为差、积、商和余数。