• 10-04-2012
Noor
I am trying to convert units in order to calculate BMI. I am having some difficulties and I would appreciate it if someone can guide me.

Code:

```#include <stdio.h> #include <math.h> int main(void) { char weight,height; double h,b; int w; printf("Enter the body weight:"); scanf("%d%s",&w,weight); printf("Enter the height:"); scanf("%lf%s",&h,height); if(weight==lb && height==ft){     b=floor((double)w*0.4536)/(pow((h*3.28),2)); printf("\nThe Body Mass Index (BMI) is: %.1f\n",b); }else if(weight==lb && height==in){     b=floor((double)w*0.4536)/(pow((h*39.37),2)); printf("\nThe Body Mass Index (BMI) is: %.1f\n",b); }else if(weight==kg && height==ft){     b=floor((double)w)/(pow((h*3.28),2)); printf("\nThe Body Mass Index (BMI) is: %.1f\n",b); }else if(weight==kg && height==mt){     b=floor((double)w)/(pow((h*1),2));     printf("\nThe Body Mass Index (BMI) is: %.1f\n",b); }else if(weight==kg && height==in){     printf("\nThe Body Mass Index (BMI) is: %.1f\n",b);     b=floor((double)w)/(pow((h*39.37),2)); }else if(weight==lb && height==mt){     b=floor((double)w*0.4536)/(pow((h*1),2)); printf("\nThe Body Mass Index (BMI) is: %.1f\n",b); } printf("Category:"); if(b<16.5) { printf("Severly underweight\n"); } else if( 16.5<=b && 18.5>b) { printf("underweight\n"); } else if( 18.5<=b && 25.0>b) { printf("Normal\n"); } else if(25.0<=b && 30.0>b) { printf("Overweight\n"); } else if(30.0<=b) { printf("Obese\n"); } return 0; }```
• 10-04-2012
ervinako
Code:

`#include <stdio.h>`
:)
• 10-04-2012
oogabooga
I don't have the words to express the idiocy of "help me" questions like this.

EDIT: To clarify, I wasn't referring to the typo (which I assumed was a copy/paste error) but to the utterly moronic post. Essentially it's just "help me, my program's not working". No other information. Not the slightest bit of effort on the OP's part.
• 10-04-2012
TheBigH
Making and overlooking a little typo is easy to do. Not seeing it when the compiler tells you there's something wrong on line 1 is harder to understand.
• 10-04-2012
Noor
i have the first part included its just when i copied and pasted...
• 10-04-2012
TheBigH
You've declared the variables "height" and "weight" as chars, but you're trying to scan them in as strings (that is, arrays of chars).

In line 16, this is NOT how to compare two strings. Use the strcmp function.
• 10-04-2012
Click_here
Your variables hight and weight are not strings, they are single characters -> You are looking for a character array.

To compare strings in those if statements, use strcmp() from <string.h>
• 10-05-2012
Doesn't it just feel wrong to have height and weight as chars?

Maybe have them as ints, maybe floats, maybe even doubles, but CHARS??

I can't get that choice of data type to feel right for height or weight.
• 10-05-2012
jsuite
Besides that, your if statement isn't doing anything. There's no way weight == lb and height == ft if there's no sort of pre-existing conditions over the ft and lb variables. To start, they aren't even defined. Could be wrong?
• 10-05-2012
ledow
1) Compile with warnings enabled. Not only will it catch typos but it will also flag absolute rubbish.

2) This:

Code:

`if(weight==lb && height==ft)`
compares the value of variable weight to the value of variable lb, and the value of variable height to the value of variable ft. Two of those don't exist in your code. I'm presuming you're expecting some magic where the program will magically determine the user was referring to feet and inches and do all the hard work for you. Sorry. Doesn't work like that. Try using some string comparisons instead on the variables you DID scanf in instead.

3) Why are you using pow() to calculate something squared (something multiplied by itself)?

4) Nest your damn if's - it's impossible to tell what's inside an if and what's not.