我不知道我在做什么错在这里,每次我运行它时,它会通过if
部分,即使它不是真的?所以'其他'从未运行。如果声明运行时即使不是真的
#include <iostream>
using namespace std;
string choice;
float numb;
float convert(float numb,string choice)
{
float newNo;
if (choice == "F" or "f"){
newNo = numb * 0.2 * 9 + 32;
}else{
newNo = (numb - 32)/1.8;
}
return newNo;
}
int main()
{
cout << "welcome to Temperature converter v0.1" << endl;
cout << endl;
cout << "Which conversion would you like to use?" << endl;
cout << "type C to convert to Celsius and F to convert to Fahrenheit - ";
cin >> choice;
cout << "what number would you like to convert? - ";
cin >> numb;
cout << convert(numb,choice);
return 0;
}
'if(choice ==“F”or“f”){'不会做你认为的事情。编写有效的C++语法以从您的代码中获得预期的行为。 –
正如@πάνταῥεῖ所说的,你误解了“if”语句中符号的语法和含义。您需要阅读比较和运算符优先级。提示,“f”总是如此。 –
'=='运算符出现在'或'运算符之前,因此它与'if((choiceOfF“)或(”f“))'相同。字符串'“f”',被评估为布尔值,总是为真,因为它不是NULL指针(可能没有完美解释,但或多或少) – hoijui