Home > Software design >  String not working with if else statement
String not working with if else statement

Time:09-16

I'm creating a BMI calculator that works in the English and Metric systems. I'm not really sure what's going on, but no matter what I input it only goes to the first conditional.

#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");
}

CodePudding user response:

On lines 17 and 22, you must include the name of the variable in the second clause of the OR operator. E.g.

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

Without this, the bare string Metric will evaluate to true, because it is non-zero.

  • Related