in reply to Re: What is wrong with this code?
in thread What is wrong with this code?

Thank You for the code. I will get back to you on the bugs (if any). The reason I didn't make it too easy to use is because I wanted to make sure the basic parts of the code worked first. I am looking to add more to it (and use some of your tips) but for now, here is my finished code:

Oh, and by the way. I know I didn't make that as simple as it can be. I'm working on that. I just wanted to show you guys some of the fixes.

use 5.14.1; use Math::Trig; ###################################################################### +##### my $square_side= shift; ##SQUARE AREA FORMULA my $square_area= $square_side*2; ###################################################################### +##### my $rectangle_side=shift; ##RECTANGLE AREA FORMULA my $rectangle_side_2=shift; my $rectangle_area = $rectangle_side*$rectangle_side_2; ###################################################################### +##### my $paralellogram_base=shift; ##PARALELLOGRAM AREA FORMULA my $paralellogram_height= shift; my $paralellogram_area=$paralellogram_base*$paralellogram_height; ###################################################################### +##### my $trapezoid_base=shift; ##TRAPEZOID AREA FORMULA my $trapezoid_base_2=shift; my $trapezoid_height=shift; my $trapezoid_area=$trapezoid_height/2*($trapezoid_base*$trapezoid_bas +e_2); ###################################################################### +##### my $circle_radius=shift; ##CIRCLE AREA FORMULA my $pi=pi; my $circle_area=$pi*($circle_radius**2); ###################################################################### +###### my $triangle_base=shift; ##NON-EQUALATERAL TRIANGLE FORMULA my $triangle_height=shift; my $triangle_area=.5*($triangle_base*$triangle_height); ###################################################################### +###### say "I am an area calculator. I can calculate the area of most basic s +hapes (mostly).\n"; say "\nWhat type of shape am I working with?\n"; my $type=<>; chomp ($type); if ($type eq 'square' or $type eq 'Square'){ say "Okay. I am working with a $type.\n"; say "ENTER SIDE LENGTH"; $square_side= <>; my $square_area= $square_side*2; say "ANSWER= $square_area"; } elsif ($type eq 'rectangle' or $type eq 'Rectangle'){ say "Okay. I am working with a $type.\n"; say "ENTER FIRST SIDE LENGTH"; $rectangle_side=<>; say "ENTER SECOND SIDE LENGTH"; $rectangle_side_2=<>; $rectangle_area = $rectangle_side*$rectangle_side_2; say "ANSWER= $rectangle_area";} elsif ($type eq 'paralellogram' or $type eq 'Paralellogram'){ say "Okay. I am working with a $type.\n"; say "ENTER BASE"; $paralellogram_base=<>; say "ENTER HEIGHT"; $paralellogram_height=<>; $paralellogram_area=$paralellogram_base*$paralellogram_height; say "ANSWER= $paralellogram_area"; } elsif ($type eq 'trapezoid' or $type eq 'Trapezoid'){ say "Okay. I am working with a $type.\n"; say "ENTER FIRST BASE"; $trapezoid_base=<>; say "ENTER THE SECOND BASE"; $trapezoid_base_2=<>; say "ENTER HEIGHT"; $trapezoid_height=<>; $trapezoid_area=$trapezoid_height/2*($trapezoid_base*$trapezoi +d_base_2); say "ANSWER= $trapezoid_area"; } elsif ($type eq 'circle' or $type eq 'Circle'){ print "Okay. I am working with a $type. \n"; say "ENTER RADIUS"; $circle_radius=<>; my $c_answer= ($circle_radius**2)*3.14; say "ANSWER= $c_answer"; } elsif ($type eq 'triangle' or $type eq 'Triangle'){ print "Okay. I am working with a $type.\n"; say "\n ENTER BASE"; $triangle_base=<>; say "ENTER HEIGHT"; $triangle_height=<>; $triangle_area=.5*($triangle_base*$triangle_height); say "ANSWER= $triangle_area"; }else{ die "I do not have that shape programmed in to my system (yet).Ple +ase tell my creator to add the function you have attempted to use.\n\ +n\n\n"; }

perl.j-----A Newbie To Perl

Replies are listed 'Best First'.
Re^3: What is wrong with this code?
by flexvault (Monsignor) on Jul 24, 2011 at 18:54 UTC

    Glad you picked up on the use of 'elsif'. Used properly, it's a very powerful tool and you used it correctly. It can help you find all possible combinations that are acceptable ( just like you did ).

    Now this is my preference, since I do a lot of web cgi programming, is to not overuse the 'die' function. You used it correctly, but I prefer to have a 'sub DieRtn{}' that is called in a error situation. This routine can determine the severity of the error and provide a way to recover. Once you 'die', it's not easy to recover.

    And if this was a cgi script, the 'die' error would just be shown on the browser window. This wouldn't be very friendly, when a simple "I don't understand. . ." would allow the user to try it again.

    Now look at this from your user's (brother) point of view. He has to type in the command and then type the shape and then the size(s) and then he sees "I do not ...". What you want as the designer/programmer of this script is for him to use this as much as possible. That was the purpose of the loop:

    while ( 1 ) ## You may prefer while ( 1==1 ) { Do your stuff . . . elsif ( $type eq '' ) { last; } else { ... notify of input not correct and show what's acceptable . + . . } }
    Now you have a clean exit to the program and you have reduced the typing for your end-user. If this is for a typing class and not a math class, then testing his typing skills would be a good thing.

    Keep up the good work...Ed

    "Well done is better than well said." - Benjamin Franklin