Thread: BMI calculator part #2 with added function

  1. #1
    Registered User jaaaaaamie's Avatar
    Join Date
    Aug 2013
    Location
    San Diego, California
    Posts
    10

    Cool BMI calculator part #2 with added function

    HI...getting error with gcc under OSX.

    Thanks


    Code:
    /* bmi2.c */
     
    #include <stdio.h>
     
    main()
    {
        float height, weight;
    
        printf("Enter weight:\n ");
        scanf("%f", &weight);
     
        printf("Enter height in inches:\n ");
        scanf("%f", &height);
     
        bmiCalc(height, weight);
     
        return 0;
    }
    
    
    bmiCalc(float height, float weight)
    {
        float bmi;
        bmi = (weight * 703) / (height * height);
        printf("Your BMI is %.2f\n", bmi);
    
    
        return 0;
    }
    Last edited by jaaaaaamie; 08-17-2013 at 02:38 PM.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    If you want a function to be declared after the main function, be sure to include the fuction prototype before any call of the function
    also
    the fuction bmiCalc has no return type but you asigned it a return value in line 28
    this should work
    Code:
    #include <stdio.h>
     
    /*This is the function prototype declared before the main function. also i have given it a return *
    *  type of void since it souldn't return anything to the user*/
    void bmiCalc(float height, float weight); 
    
    
    main()
    {
        float height, weight;
     
        printf("Enter weight:\n ");
        scanf("%f", &weight);
      
        printf("Enter height in inches:\n ");
        scanf("%f", &height);
      
        bmiCalc(height, weight);
      
        return 0;
    }
     
     
    void bmiCalc(float height, float weight)
    {
        float bmi;
        bmi = (weight * 703) / (height * height);
        printf("Your BMI is %.2f\n", bmi);
    //There is no return 0; here because the return type of the bmiCalc is void
    }
    Hope this helps

  3. #3
    Registered User jaaaaaamie's Avatar
    Join Date
    Aug 2013
    Location
    San Diego, California
    Posts
    10
    thanks...i saw this in my c book. I will refer to it and re-read it. thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator Help part 2
    By gnomelook in forum C Programming
    Replies: 5
    Last Post: 01-08-2012, 07:57 AM
  2. send one part of struct array to function
    By cable in forum C Programming
    Replies: 11
    Last Post: 09-22-2011, 12:54 PM
  3. Making a function like this part of a class
    By jamort in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2011, 04:58 PM
  4. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  5. new function added
    By dune911 in forum C Programming
    Replies: 3
    Last Post: 01-25-2002, 10:28 AM