Thread: Help with simple BMI calculator in C

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    5

    Help with simple BMI calculator in C

    So I am fairly new to coding in general and one of my assignments was to make a working BMI calculator. I don't get any errors but when i run the program my bin.exe crashes every time I type a number in when asked "What is your height in inches". Does anyone know why this would happen or see an error in my code which I will put below? Thanks

    Code:
    /* My Name
     * 8.29.14
     * To calculate the BMI of the user
     */
    
    
    //included libraries
    #include <stdio.h>
    #include <math.h>
    
    
    //main function
    int main() {
    
    
        //Variables
        float height,weight,BMI;
    
    
        //Ask user for data
        printf("What is your height in inches?\n", height);
        scanf("%d, &height");
    
    
        printf("What is your weight in pounds?\n", weight);
        scanf("%d, &weight");
    
    
        //bmi equation
        BMI = (weight * 703) / (height * height);
        printf("Your BMI is %.2f\n, BMI");
    
    
    
    
        return 0;
    }
    Last edited by Bponczek; 08-29-2014 at 01:23 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%d, &height");
    
    // ...
    
    scanf("%d, &weight");
    &height and &weight should not be within the quotes.

    Code:
    printf("Your BMI is %.2f\n, BMI");
    Neither should BMI.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    5
    Thank you so much, that did the trick, now the BMI is coming out to "Your BMI is 1.#J" probably something with my equation

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "height" and "weight" are declared as floats. The format specifiers in your "scanf()" calls are looking for integers.

    You should be getting compiler warnings about these things:

    Code:
    /*
    main.c||In function 'main':|
    main.c|22|warning: format '%d' expects type 'int *', but argument 2 has type 'float *'|
    main.c|26|warning: format '%d' expects type 'int *', but argument 2 has type 'float *'|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    If not, read your compiler documentation to find out how to increase the warning level.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    also, you should use %f in your call to scanf. %d is for int types.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    5
    Got it all working, just changed the %d to %f, thank you again

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. Just FYI, it's not a good idea to edit your original post with corrections, since this makes the subsequent posts appear to make less sense. It's good that you posted the corrected code, but this should be done with a follow-up post. No big deal, though.

    Oh, and welcome to the forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] calculato program
    By spirit2244 in forum C Programming
    Replies: 10
    Last Post: 10-22-2012, 09:16 AM
  2. Simple Socket Library Not so simple
    By jbully in forum C Programming
    Replies: 4
    Last Post: 12-23-2010, 09:23 AM
  3. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  4. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  5. Replies: 1
    Last Post: 07-20-2002, 06:33 PM