# Thread: Convert IBW to pounds.

1. ## 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. I note that you wrote (Gender = 1). Did you intend to write (Gender == 1)?

3. That did not seem to change anything on the examples I tried.

4. 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.

5. 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

6. 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.

7. 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. Okay, here are a few tips:
• When posting code, don't post line numbers.
• 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?

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.

9. > 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?

10. 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. 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'.

12. 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. 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'.