code.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Body Mass Index (BMI) is found by taking your weight
  2. * in kilograms and dividing by the square of your height in meters.
  3. * The BMI categories are:
  4. * Underweight: <18.5
  5. * Normal weight: 18.5–24.9
  6. * Overweight: 25–29.9
  7. * Obesity: BMI of 30 or greater
  8. * Create a function that will accept weight and height
  9. * (in kilos, pounds, meters, or inches) and return the BMI
  10. * and the associated category. Round the BMI to nearest tenth.
  11. * 1 inch = .0254 meter
  12. * 1 pound = 0.453592 kilo
  13. */
  14. #include <cmath>
  15. #include <iomanip>
  16. std::string BMI(std::string weight, std::string height) {
  17. // stoi weight & height to isolate integers
  18. float w = std::stof(weight /*size_t* idx = 0, int base = 10*/);
  19. float h = std::stof(height);
  20. // find pounds or feet to determine if a conversion needs to happen
  21. if(std::string::npos != weight.find("pounds")){
  22. w = w * 0.453592;
  23. }
  24. if(std::string::npos != height.find("inches")){
  25. h = h * .0254;
  26. }
  27. // bmi is weight in kilograms and dividing by the square of your height in meters
  28. std::string bmi = std::to_string(round(10*(w/(h*h)))/10);
  29. // find decimal point position
  30. int pos = bmi.find(".");
  31. // substring decimal location + 1
  32. bmi = bmi.substr(0,pos+2);
  33. float bmiFloat = round(10*(w/(h*h)))/10;
  34. if(18.5>bmiFloat){
  35. return bmi + " Underweight";
  36. }
  37. if(24.9>=bmiFloat){
  38. return bmi + " Normal weight";
  39. }
  40. if(29.9>=bmiFloat){
  41. return bmi + " Overweight";
  42. }
  43. if(30<=bmiFloat){
  44. return bmi + " Obesity";
  45. }
  46. }