Thread: Convert IBW to pounds.

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

    Convert IBW to pounds.

    In the IBW calculation, the male is off by -10lbs on any given example and if I flip the male and female aspects the female is off by +10lbs

    It must be something small I just need a second look at it. thanks!

    Code:
      //Compute Ideal Body Weight Given Height and Gender & Convert IBW to pounds.  
     90       
     91 
     92    char F = 1;
     93 
     94       if (Gender = 1)
     95       {  
     96          IBW=(HEIGHT-60)*2.3+45.5;
     97          IBW=IBW*2.2046;
     98       }
     99       
    100       else 
    101       {
    102          IBW=(HEIGHT-60)*2.3+50.0;
    103          IBW=IBW*2.2046;
    104       }
    105       
    106       
    107       // Display IBW.
    108 
    109       printf("The ideal weight is %5.2f.", IBW);
    110    
    111    return 0;
    112    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that you wrote (Gender = 1). Did you intend to write (Gender == 1)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    That did not seem to change anything on the examples I tried.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the examples that you tried? Post the smallest and simplest compilable program that demonstrates the problem. Post also the test input, actual output and expected output. Post the mathematical formula involved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    Code:
    Enter the height in inches.
    72
    Enter the weight in pounds.
    165
    
    The BMI is 22.38
    BMI classification: Normal
    
    Enter the target BMI
    25
    The target weight is 184.33.
    
    Is the person Female or Male? Enter F or M:
    M
    The ideal weight is 171.08.
     ----jGRASP: operation complete.
    
    Enter the height in inches.
    59
    Enter the weight in pounds.
    
    100
    The BMI is 20.20
    BMI classification: Normal
    
    Enter the target BMI
    25
    The target weight is 123.78.
    
    Is the person Female or Male? Enter F or M:
    F
    The ideal weight is 105.16.
    ----jGRASP: operation complete.
    

    Correct:

    ht(in) wt(lbs) BMI TargetBMI wt(lbs) F/M IBW
    72 165 22.378 25 184.333 M 171.08
    59 100 20.20 25 123.78 F 95.24


    Last edited by drewmail; 02-08-2012 at 11:11 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That does not help. All I can say is that the calculations look correct to me, because I don't know what "correct" or "wrong" looks like to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    The correct ideal body weight should be 95.24 on a female not 105.16. The male, on the other hand, is correct. Thanks for your attempt anyway

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, here are a few tips:
    • When posting code, don't post line numbers.
    • Indent your code consistently.
    • When naming variables, avoid naming them fully capitalised. Reserve fully capitalised names to macro names (or constants).


    Next, consider your formula has a bunch of magic numbers. The thing that varies is HEIGHT, so what's with the weight input?

    Quote Originally Posted by drewmail
    The correct ideal body weight should be 95.24 on a female not 105.16.
    Here's a test program that I wrote:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int HEIGHT = 59;
        double IBW;
        IBW=(HEIGHT-60)*2.3+45.5;
        IBW=IBW*2.2046;
    
        printf("The ideal weight is %5.2f.\n", IBW);
    
        return 0;
    }
    The output that I get is: 95.24

    In other words, I cannot replicate your problem.

    EDIT:
    Actually, I can replicate your problem by substituting the other formula. Let me just say this straight out: when you posted your post #3, you failed to adequately test your program. My post #2 was the hint that you needed.
    Last edited by laserlight; 02-08-2012 at 11:22 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > That did not seem to change anything on the examples I tried.
    Are you sure you recompiled the code?
    Are you sure you saved the file before recompiling?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    Code:
     
    
    12 #include <stdio.h> 13 #include <math.h>
     14 
     15 int main()
     16 {     
     20 
     21    double BMI, HEIGHT, WEIGHT, TargetBMI, IBW;
     22    char Gender;
     23 
     27          printf("Enter the height in inches.\n");
     28          scanf("%lf", &HEIGHT);
     29       
     30          printf("Enter the weight in pounds.\n\n");
     31          scanf("\n%lf", &WEIGHT);
     32       
     33       // to metric
     34    
     35          {  
     36             WEIGHT = WEIGHT/2.2046;
     37          
     38             HEIGHT = HEIGHT*0.0254;
     39          }
          // Calculate BMI
     42       
     43          BMI = WEIGHT/(HEIGHT*HEIGHT); 
     44          
     47    
     61       
     62       // Ask for target BMI
     63    
     64       printf("Enter the target BMI\n");
     65       scanf("\n%lf", &TargetBMI);
     66    
     67       // Compute Ideal Body Weight(IBW)
     68       
     69       {
     70          TargetBMI = TargetBMI*HEIGHT*HEIGHT;
     71       
     72          TargetBMI = TargetBMI*2.2046;
     73       }
     74    
     75       // Display Ideal Body Weight(IBW)
     76 
     77       printf("The target weight is %5.2f.\n\n", TargetBMI);
     78    
     79       // Prompt user to enter the gender (F or M).
     80    
     81       printf("Is the person Female or Male? Enter F or M:\n");
     82       scanf("\n%c", &Gender);
     83       
     84       //Returning HEIGHT variable to inches
     85       
     86    HEIGHT=HEIGHT/.0254;
     87    
     88       
     89       //Compute Ideal Body Weight Given Height and Gender & Convert IBW to pounds.  
     90       
     91 
     92    char F = 1;
     93 
     94       if (Gender = 1)
     95       {  
     96          IBW=HEIGHT-60;
     97          IBW=IBW*2.3+45.5;
     98          IBW=IBW*2.2046;
     99       }
    100       
    101       else 
    102       {
    103          IBW=(HEIGHT-60)*2.3+50.0;
    104          IBW=IBW*2.2046;
    105       }
    106       
    107       
    108       // Display IBW.
    109 
    110       printf("The ideal weight is %5.2f.", IBW);
    111    
    112    return 0;
    
    113    }


    Here is what I have.. if that helps you better understand what is happening.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change (Gender = 1) to (Gender == 1), compile and test for both male and female. Report your results.

    Here's the thing: (Gender = 1) assigns 1 to Gender and evaluates as true, so this code:
    Code:
    if (Gender = 1)
    {  
        IBW=HEIGHT-60;
        IBW=IBW*2.3+45.5;
        IBW=IBW*2.2046;
    }
    else 
    {
        IBW=(HEIGHT-60)*2.3+50.0;
        IBW=IBW*2.2046;
    }
    Effectively becomes:
    Code:
    Gender = 1;
    IBW=HEIGHT-60;
    IBW=IBW*2.3+45.5;
    IBW=IBW*2.2046;
    Furthermore, Gender is a char. Why are you trying to compare it with 1? You should be comparing with 'M' or 'F'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    Code:
    char F; 
             if (Gender == F)
          {  
             IBW=HEIGHT-60;
             IBW=IBW*2.3+45.5;
             IBW=IBW*2.2046;
          }
    10       
    10       else 
    10       {
    10          IBW=(HEIGHT-60)*2.3+50.0;
    10          IBW=IBW*2.2046;
    10       }
    10       
    10       
    10       // Display IBW.
    10 
          printf("The ideal weight is %5.2f.", IBW);
    Still having the same results.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Increase the warning level of your compiler. You compare Gender with F, but F was not initialised. It could well have the value of... 'M'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Conversion Table- Pounds to Dollars
    By phillysbest29 in forum C Programming
    Replies: 3
    Last Post: 09-23-2009, 08:37 PM
  2. Convert .lib to .a
    By maxorator in forum Tech Board
    Replies: 2
    Last Post: 11-30-2006, 10:47 AM
  3. convert maybe maybe not..
    By pico in forum C Programming
    Replies: 7
    Last Post: 03-15-2005, 10:13 AM
  4. Convert .OMG to .MP3
    By RoD in forum Tech Board
    Replies: 3
    Last Post: 05-31-2004, 10:55 PM
  5. Convert WAV to MID
    By E i F x 65 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 02:17 PM