I have been working on some code that calculates your BMI and then puts you into a category based on that. every time i try and run this program it either tells me that I am in the underweight class or the average class and I have no idea why please help

this is the part of the code that sorts the BMI to the correct category

if(weight <= 18.4){
        cout << "Your BMI is " << weight << " you are underweight." << endl;
        getch();
        return 0;
        }
        
 if(weight <= 24.9 || weight >= 18.5){
        cout << "Your BMI is " << weight << " you are average." << endl;
        getch();
        return 0;
        }
        
 if(weight <= 29.9 || weight >= 25.0){
        cout << "Your BMI is " << weight << " you are overweight." << endl;
        getch();
        return 0;
        }
        
 if(weight >= 30.0){
        cout << "Your BMI is " << weight << "you are Obese." << endl;
        getch();
        return 0;
        }

Thanks in advance

Recommended Answers

All 3 Replies

Hello gladtoplay5,
You are getting that because you have used the || logical operator. It should have been &&.

if(weight <= 24.9 || weight >= 18.5){
        cout << "Your BMI is " << weight << " you are average." << endl;
        getch();
        return 0;
        }

Here if you give 16 as input, then 16 is less than 18.5 so it will print "You are underweight", In the next if condition 16 is less than 24.5 so "you are average" will also printed.

Thank you for the help

Actually, you don't need the && at all. And you can shorten your code dramatically by looking more carefully at it:

if (weight <= 18.4)
    {
        cout << "Your BMI is " << weight << " you are underweight." << endl;
    }
    else     
    if (weight <= 24.9)  // weight cannot be less than 18.4
    {
        cout << "Your BMI is " << weight << " you are average." << endl;
    }
    else     
    if (weight <= 29.9)  // weight cannot be less than 24.9
    {
        cout << "Your BMI is " << weight << " you are overweight." << endl;
    }
    else     
    if (weight >= 30.0)
    {
        cout << "Your BMI is " << weight << "you are Obese." << endl;
    }

    getch();    // All your tests do this, so do it once.
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.