C++计算器跳过其他语句
C++ Calculator Skipping Else Statement
我正在用C++制作一个简单的计算器。但是,该程序并没有完全按照应有的方式运行。运行时,trig if 语句执行正常,但是,基本算术 else 语句不起作用。我已经确定代码没有执行 else 语句,并且想知道如何解决它。else 语句中的代码工作正常,因为我已注释掉 if 语句。帮助?
这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
int main()
{
double input = 0;
double firstnumber = 0;
double secondnumber = 0;
std::string function;
std::string operation;
std::cout << "Enter your calculation: ";
std::cin >> function;
if(function == "sin" || "cos" || "tan")
{
if(function == "sin")
{
std::cin >> input;
std::cout << "The sine is " << sin(input) << std::endl;
system("PAUSE");
}
else if(function == "cos")
{
std::cin >> input;
std::cout << "The cosine is " << cos(input) << std::endl;
system("PAUSE");
}
else if(function == "tan")
{
std::cin >> input;
std::cout << "The tangent is " << tan(input) << std::endl;
system("PAUSE");
}
}
else
{
firstnumber = ::atof(function.c_str());
std::cin >> operation;
std::cin >> secondnumber;
double valueadd = firstnumber + secondnumber;
double valuesubtract = firstnumber - secondnumber;
double valuemultiply = firstnumber * secondnumber;
double valuedivide = firstnumber / secondnumber;
if(operation == "+")
{
std::cout << " = " << valueadd << std::endl;
system("PAUSE");
}
else if(operation == "-")
{
std::cout << " = " << valuesubtract << std::endl;
system("PAUSE");
}
else if(function == "*")
{
std::cout << " = " << valuemultiply << std::endl;
system("PAUSE");
}
else if(function == "/")
{
std::cout << " = " << valuedivide << std::endl;
system("PAUSE");
}
else
{
std::cout << "Error" << std::endl;
return 0;
}
}
return 0;
}
这一行是错误的。
if(function == "sin" || "cos" || "tan")
它应该是
if((function == "sin") || (function == "cos") || (function == "tan"))
请注意,检查实际上毫无意义,因为您已经单独检查了它们。你可以通过在if
、else if
、else
链中执行此操作来整理它。
您必须单独写出每个条件。以下代码行可以编译,但它不会按照您的想法执行:
if (function == "sin" || "cos" || "tan")
将其更改为以下内容:
if (function == "sin" || function == "cos" || function == "tan")
由于你想为每个trig函数做一些不同的事情,你应该只有一个if...else if...else if...else if...else
链。没有必要像现在这样嵌套 if 语句。事实上,它可能效率较低,因为您需要检查每个条件两次。
更改:
if(function == "sin" || "cos" || "tan")
到:
if ((function == "sin") || (function == "cos") || (function == "tan"))
您拥有的首先计算表达式"sin" || "cos" || "tan"
,然后尝试将字符串与该字符串进行比较。
但是,事实上,实际上没有必要进行这两个步骤。您可以简单地执行以下操作:
if (function == "sin") {
std::cin >> input;
std::cout << "The sine is " << sin (input) << std::endl;
system ("PAUSE");
} else if (function == "cos") {
std::cin >> input;
std::cout << "The cosine is " << cos (input) << std::endl;
system ("PAUSE");
} else if (function == "tan") {
std::cin >> input;
std::cout << "The tangent is " << tan (input) << std::endl;
system ("PAUSE");
} else {
// It's neither sin, cos nor tan if you get here.
firstnumber = ::atof (function.c_str ());
// and the rest of your stuff in here.
}
相关文章:
- C++ If 语句,从其他语句重复
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 如果有 constexpr if 语句,为什么没有其他 constexpr 语句呢?
- 在后台循环,同时运行其他语句 C++
- C - 如果语句问题.代码将不会运行其他,否则
- 否则,如果语句意外执行,则表现为其他语句
- 我可以将循环用作如果其他条件语句
- 如何引用无效的输入,然后重新输入和如果其他语句
- C 程序运行不正确的其他语句
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- 如何将多个变量与其他if语句进行比较
- 如何在内联回调函数中使用EXPECT_EQ和其他Google Mock语句
- C 使用功能定义中的if/其他语句以返回最小的数字
- 最后一行欧普特两个组合输出问题?如果其他语句
- 我的C 代码永远不会超过其他语句
- 为什么if语句中未定义的参数不会导致segfault或其他错误
- 如何停止 while 循环并转到我的其他语句
- C++Bare布尔表达式(不包含if、while或其他语句)
- C++,其他受scanf语句影响的变量
- C++计算器跳过其他语句