Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Body Mass Index Calculator

by naildownx (Beadle)
on Jan 29, 2011 at 02:17 UTC ( #884961=CUFP: print w/ replies, xml ) Need Help??

Instead of having to do this on paper and a calculator I decided to make a sweet Perl script to do this for me. Any comments are welcomed since I don't claim to be a Perl expert by any means. Enjoy!

#!/usr/bin/perl use strict; use warnings; use 5.010; # BMI Calculator For Men # Created by: Jamie Newton # 12-11-10 Formula Cited Sources # The Body Sculpting Bible for Men, Hugo Rivera & James Villepique # Introduction ------------------------------------------------------- +------------- say "BMI CALCULATOR FOR MEN\n\nHello, this tool will calculate your to +tal fat percentage."; # Declare Variables to be used in Script my($current_weight); my($waist_girth); my($BodyShape); # Collect User's Weight ---------------------------------------------- +------------- do { say "Please enter your current weight >"; chomp($current_weight = <STDIN>); } until ($current_weight =~ /^[0-9]+$/); # Collect User's Waist Size ------------------------------------------ +------------- do { say "Please enter your waist girth (measured at the umbilicus) + >"; chomp($waist_girth = <STDIN>); } until ($waist_girth =~ /^-{0,1}\d*\.{0,1}\d+$/); # Get Lean Body Weight (as if you had no fat at all) ----------------- +-------------- my $Result1 = ($current_weight * 1.082) + 94.42; my $Result2 = $Result1 - ($waist_girth * 4.15); # Round to 2 decimal Places ------------------------------------------ +-------------- my $BodyFatPercentage = sprintf "%.2f"; # Find Out The Total Body Fat Percentage ----------------------------- +-------------- chomp($BodyFatPercentage = (($current_weight-$Result2) * 100) / $curre +nt_weight); # Determine if the User is Underweight, Healthy, Overweight, or Obese if ($BodyFatPercentage < 18) { $BodyShape = "Underweight"; } elsif ($BodyFatPercentage > 17 && $BodyFatPercentage < 25) { $BodyShape = "Healthy"; } elsif ($BodyFatPercentage > 24 && $BodyFatPercentage < 30) { $BodyShape = "Overweight"; } elsif ($BodyFatPercentage >= 30) { $BodyShape = "Obese"; } #Display BF % to User print "\nYour Total Body Fat % is "; print sprintf("%.2f", $BodyFatPercentage); print "%\n\n"; print "This indicates that you are $BodyShape."; print "\n\n"; print "Extra: Your weight without any fat = $Result2"; print "\n\n\n\n\n";

The early bird gets the worm but the second mouse gets the cheese.
pretendeavor

Comment on Body Mass Index Calculator
Download Code
Re: Body Mass Index Calculator
by toolic (Chancellor) on Jan 29, 2011 at 03:05 UTC
    My comments have nothing to do with Perl.

    That is not a definition of BMI that I am familiar with. Normally, it is:

    mass/(height)^2

    I think it is important for you to specify units in your prompts. I entered my weight in pounds and my waist in inches -- and your code has declared me obese! Considering a friend recently likened my physique to that of a typical Boston Marathon champion, I'd say something is definitely askew.

      First of all, I agree with you, that it is not a very accurate indicator of a true BMI measurement. Secondly, I had not thought about what units others may use because the book was American written and distributed, thanks for pointing that out. And finally, I only use the number as an indication of progress, not an actual reading of true BMI, only calipers can indicate that. Plus, I took the equations straight from the book, so if you have qualms with their formulated ways of arriving at a BMI % then write those authors. Thanks for the reply and taking a look at the script :)
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor
Re: Body Mass Index Calculator
by GrandFather (Cardinal) on Jan 29, 2011 at 04:51 UTC

    I see no indication of expected units for length or weight.

    The following comments don't really affect the utility of the script, but are areas you may wish to look at to clean the code up somewhat:

    print sprintf can be replaced by printf.

    Why is $waist_girth allowed to be negative?

    Declarations for single variables don't need to be in a list. Instead just: my $BodyShape;. Even better, declare variables where they are first used ($BodyShape isn't used until half way through the program).

    my $BodyFatPercentage = sprintf "%.2f"; does nothing. sprintf will generate '0.00' (and a "Use of uninitialized value" warning) because of the missing parameter required by the %f format item. The chomp in the subsequent line does nothing because the calculation does not append a new line character.

    $waist_girth =~ /^-{0,1}\d*\.{0,1}\d+$/ allows '.0' but not '0.'.

    Update: corrected sprintf comment (thanks ambrus - though I was sure I'd checked it!).

    True laziness is hard work
      Love your signature GrandFather :)
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor
Re: Body Mass Index Calculator
by wazoox (Prior) on Jan 30, 2011 at 21:25 UTC
    Unfortunately it doesn't make any sense with rational measurement units (kilograms an centimeters) :
    Please enter your current weight > 87 Please enter your waist girth (measured at the umbilicus) > 98 Your Total Body Fat % is 350.74% This indicates that you are Obese. Extra: Your weight without any fat = -218.146
    I fail to understand what the calculations are about to apply proper conversion.
      I plan on modifying this script as soon as I get a chance to try to accommodate these changes that have been requested...I feel like it could be much better...
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://884961]
Approved by ww
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-07-12 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (236 votes), past polls