# Not getting expected Output

• 02-07-2012
ldysmann
Not getting expected Output
I am trying to calculate Body mass index and body weight category based on height and weight. The BMI works but it always gives me "underweight" for some reason. Ay help is much appreciated!

Code:

```#include <iostream> using namespace std; int main() {     double hgt=0; double wgt=0; double BodyMI=0; { cout<<"Enter Height"<<endl; cin>>hgt; cout<<"Enter Weight"<<endl; cin>>wgt; } if(BodyMI<=18.4) { BodyMI=703 * wgt / (hgt * hgt); cout<<"BMI:"<<BodyMI<<endl; cout<<"Body Weight Category:Underweight"<<endl; } else if(BodyMI>=18.5 && BodyMI<=24) { BodyMI=703 * wgt / (hgt * hgt); cout<<"BMI:"<<BodyMI<<endl; cout<<"Body Weight Category:Normal"<<endl; } else if(BodyMI>=25&&BodyMI<=29) {  BodyMI=703 * wgt / (hgt * hgt); cout<<"BMI:"<<BodyMI<<endl; cout<<"Body Weight Category:Overweight"<<endl; } else if(BodyMI>=30) {  BodyMI=703 * wgt / (hgt * hgt);    cout<<"BMI:"<<BodyMI<<endl; cout<<"Body Weight Category:Obese"<<endl; }  system("pause"); return 0; }```
• 02-07-2012
jimblumberg
In the following snippet, where are you computing BodyMI?

Code:

```double hgt=0; double wgt=0; double BodyMI=0; { cout<<"Enter Height"<<endl; cin>>hgt; cout<<"Enter Weight"<<endl; cin>>wgt; } if(BodyMI<=18.4)```
Jim
• 02-07-2012
ldysmann
It looks like it is not being computed until the next line down from that snippet.
• 02-07-2012
jimblumberg
Does that really make any sense? If you don't compute it until after that if statement the if will always evaluate to true, because you initialized the variable to 0. That will report Underweight, just like you are seeing, every time.

Jim
• 02-07-2012
ldysmann
It makes sense now that you put it that way. Thank you very much for your time and input!
• 02-08-2012
Elysia
Indent your code. Always indent the code you post and make sure it is indented in your source files (even more important)!