Not getting expected Output

This is a discussion on Not getting expected Output within the C++ Programming forums, part of the General Programming Boards category; I am trying to calculate Body mass index and body weight category based on height and weight. The BMI works ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,755
    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

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    It looks like it is not being computed until the next line down from that snippet.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,755
    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

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    It makes sense now that you put it that way. Thank you very much for your time and input!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Indent your code. Always indent the code you post and make sure it is indented in your source files (even more important)!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-11-2011, 08:28 PM
  2. Array output not as expected
    By Futomara in forum C Programming
    Replies: 26
    Last Post: 11-04-2010, 11:24 AM
  3. BAH! expected `;' before 'else'
    By Chef in forum C++ Programming
    Replies: 19
    Last Post: 10-03-2008, 01:37 PM
  4. expected `)' before str... help!
    By dingobiatch in forum C++ Programming
    Replies: 10
    Last Post: 05-15-2008, 10:20 AM
  5. non expected output
    By c++.prog.newbie in forum C Programming
    Replies: 2
    Last Post: 09-27-2004, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21