BMI Calculator with functions

May 8, 2017 at 5:08am
So the program runs, but it always spits out 0 for the bmi and I can't figure out why. Help is greatly appreciated!

#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;

double calculateBMI (double weight, double height)
{
double bmi= (weight * 703)/(height * height);
return bmi;
}

void displayHealthStatus (double bmi)
{
cout << "Your BMI value is " << bmi <<
"\n\nBMI VALUES \n underweight: less than 18.5"<<
"\n Normal: between 18.5 and 24.9"<<
"\n Overweight: between 25 and 29.9"<<
"\n Obese: 30 or greater" << endl;
}

int main()
{
double weight, height, bmi;

cout << "Enter your weight in pounds: ";
cin >> weight;
cout << "Enter your height in inches: ";
cin >> height;

calculateBMI(height, weight);
//BMI always comes out to zero and I don't know why
displayHealthStatus(bmi);

return 0;
}
May 8, 2017 at 5:23am
You didn't assign the value returned from calculateBMI to anything :+)

Pleas always use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Always initialise your variables to something, preferably wait until you have a sensible value to assign to it, otherwise at the same time as declaration:

1
2
3
4
5
6
double weight = 0.0;
double  height = 0.0;

// ...

double bmi = calculateBMI(height, weight);

Do 1 variable declaration per line.

Put forward declarations of the functions before main, function definitions after main. This is so main is near the top of the file.
May 8, 2017 at 5:27am
A couple of options.
You have the height and weight parameters swapped.

Option A.
This calculateBMI(height, weight); , should be this bmi = calculateBMI(weight, height);

OR

Option B.
From this...
1
2
3
calculateBMI(height, weight);
//BMI always comes out to zero and I don't know why
displayHealthStatus(bmi);


TO
displayHealthStatus(calculateBMI(weight, height));

The reason is that the function calculateBMI returns the BMI value back to the calling function.

Your choice.
May 8, 2017 at 3:59pm
Thanks guys! Everything works perfectly now
Topic archived. No new replies allowed.