1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- /* Body Mass Index (BMI) is found by taking your weight
- * in kilograms and dividing by the square of your height in meters.
- * The BMI categories are:
- * Underweight: <18.5
- * Normal weight: 18.5–24.9
- * Overweight: 25–29.9
- * Obesity: BMI of 30 or greater
- * Create a function that will accept weight and height
- * (in kilos, pounds, meters, or inches) and return the BMI
- * and the associated category. Round the BMI to nearest tenth.
- * 1 inch = .0254 meter
- * 1 pound = 0.453592 kilo
- */
- #include <cmath>
- #include <iomanip>
- std::string BMI(std::string weight, std::string height) {
- // stoi weight & height to isolate integers
- float w = std::stof(weight /*size_t* idx = 0, int base = 10*/);
- float h = std::stof(height);
- // find pounds or feet to determine if a conversion needs to happen
- if(std::string::npos != weight.find("pounds")){
- w = w * 0.453592;
- }
- if(std::string::npos != height.find("inches")){
- h = h * .0254;
- }
- // bmi is weight in kilograms and dividing by the square of your height in meters
- std::string bmi = std::to_string(round(10*(w/(h*h)))/10);
- // find decimal point position
- int pos = bmi.find(".");
- // substring decimal location + 1
- bmi = bmi.substr(0,pos+2);
- float bmiFloat = round(10*(w/(h*h)))/10;
- if(18.5>bmiFloat){
- return bmi + " Underweight";
- }
- if(24.9>=bmiFloat){
- return bmi + " Normal weight";
- }
- if(29.9>=bmiFloat){
- return bmi + " Overweight";
- }
- if(30<=bmiFloat){
- return bmi + " Obesity";
- }
-
- }
|