0

I have created a simple function that calculates the BMI of a person given the weight and height (metric). Everything works fine except when the BMI is 30. It's returning Obese instead of Overweight even though I have put less than or equal to (<=). I have found a fix for it, but I am not understanding why this fixes it. The fix I found is casting the bmi variable to an integer. Thank you!

#include <iostream>

std::string bmi(double w, double h){
    double bmi = w / (h * h);
    std::cout << bmi << '\n';
    if(bmi <= 18.5){
        return "Underweight";
    }else if(bmi <= 25){
        return "Normal";
    }else if(bmi <= 30){ // BUG, fix: cast bmi to (int), but WHY ?
        return "Overweight";
    }else{ // bmi > 30.0
        return "Obese";
    }
}

int main(){
    std::cout << bmi(86.7, 1.7) << '\n';
    return 0;
}
Vrej
  • 70
  • 1
  • 8

0 Answers0