This program is supposed to return the BMI of an athlete. As I go through it, it gets to the calculation part and it's storing the values for height and weight properly, but it won't store the result of the equation for the BMI. It keeps giving me 0. Does anyone know what's wrong? Any help would be appreciated.
#include "stdafx.h"
#include <iostream>
#include "BMI.h"
usingnamespace std;
int main()
{
BMI BMI;
cout << "This program will calculate the BMI of an athlete.\n";
cout << "Please enter the Height in inches: ";
BMI.getHeight();
cout << "Please enter the Weight in pounds: ";
BMI.getWeight();
BMI.printBMI();
return 0;
}
BMI::BMI(void)
{
weight;
height;
bmi;
}
int BMI::getHeight()
{ cin.operator>>(height);
return height;}
int BMI::getWeight()
{ cin.operator>>(weight);
return weight;}
int BMI::calculateBMI(int height, int weight)
{ int ans;
ans = (weight * (703/(height*height)));
return ans; }
void BMI::printBMI()
{ int result = calculateBMI(height, weight);
cout << "The BMI of the athlete is " << result << endl;}
BMI::~BMI(void)
{
}
It seems that height and weight are declared as integer numbers. In this case the result of expression 703/(height*height) is always equal to 0 if 703 is less that height * height.
integers are not as accurate as floating points since a < b then a/b = 0 for all a and b because yor divisor is greater than your dividend (numerator). it rounds down.