-1

I was asked to make this program to calculate a persons Body Mass Index. US_IMPERIAL is incomplete on account of my noticing METRIC not running. I can't figure out the error, as whatever numbers I input, I get 0 as a result.

#include <iostream>
using namespace std;

float METRIC (int b, int c)
{
  float res;
  res = b / c / c * 10000;
  return res;
}


float US_IMPERIAL (float d, float e)
{
  float resu;
  resu = (d / e / e) * 703;
  return resu;
}

int main () 
{
  cout << "Hello. BMI CALCULATOR!" << endl <<
    "Press 1 for METRIC or 2 for US imperial: ";
  int a;
  cin >> a;
  if (a == 1) {
    cout << "Enter your weight in KILOGRAMS (KG): ";
    int b;
    cin >> b;
    cout << "Now enter you height in CENTIMETERS(CM): ";
    int c;
    cin >> c;
    cout << "Your BMI is: " << METRIC (b, c);
  } else if (a == 2) {
    // not yet implemented
  } else {
    cout << "Error.";
  }
  return 0;
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
  • The amount of white space and the indentation of your code matters for readability. Please format your code in a consistent way to make reading it easier. – François Andrieux Aug 02 '18 at 14:35
  • 1
    Integer division strikes again. You're storing the result of an *integer division* in a floating point variable. (example of integer div: 3/4 = 0), my guess is you were expecting floating point math. – Borgleader Aug 02 '18 at 14:35
  • 1
    Possible duplicate of [Why does division result in zero instead of a decimal?](https://stackoverflow.com/questions/8906722/why-does-division-result-in-zero-instead-of-a-decimal) – irowe Aug 02 '18 at 14:49

1 Answers1

1

In the METRIC function, the expression b / c / c * 10000 is an all integer expression. That will not work very well if you want fractions.

I suggest you turn the arguments b and c into floating point variables.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621