字符串与if else语句不匹配



我正在创建一个在英制和公制系统中工作的BMI计算器。我不太确定发生了什么,但是不管我输入什么,它只会转到第一个条件。

#include <iostream>
using namespace std;


int main()
{
int weight;
int height;
string measurementSystem;
cout << "Metric or English?: n";
cin >> measurementSystem;
if (measurementSystem == "metric" || "Metric") {
cout << "Enter your weight (kg): n";
cin >>  weight;
cout << "Enter your height (m): n";
cin >> height;

}else if (measurementSystem == "english" ||"English") {
cout << "Enter your weight (lbs): n";
cin >> weight;
cout << "Enter your height (in): n";
cin >> height;

}else{

cout << "Please check spelling";
}


system("pause>0");
}

在第17行和第22行,必须在OR操作符的第二个子句中包含变量的名称。例如

if (measurementSystem == "metric" || measurementSystem == "Metric") {

如果没有这个,裸字符串Metric将求值为true,因为它是非零的。

最新更新