Thread: help me please :)

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    help me please :)

    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;
    }
    Last edited by Noor; 10-04-2012 at 10:09 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Code:
    #include <stdio.h>

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    Last edited by oogabooga; 10-04-2012 at 10:26 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    i have the first part included its just when i copied and pasted...

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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>
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 10-05-2012 at 02:34 AM.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    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. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed