in reply to What is wrong with this code?

Everyone has helped you with the technical problems of your script, but you should think about the usefulness of helping your brother(end user) use the script. Just think how annoying it is to type 'CIRCE' by accident, and nothing happens!

Use perl to help the typist, such as:

my $help = qq|I am an area calculator that can do the following:\n \t 1 - Square \t 2- Rectangle\n \t 3 - Paralellogram \t 4 - Trapezoid\n|; ## Add additional fu +nctions here! print "\n\n$help\n"; while ( 1 ) { print "What would you like to do? "; my $type=<>; chomp ($type); $type = lc($type); while ($type) { if ( ($type eq '1' )||( $type eq 'square') ) { $type = "Square"; say "Okay. I am working with a $type.\n"; say "ENTER SIDE LENGTH"; my $square_side = chomp(<>); my $square_area= $square_side**2; say "ANSWER= $square_area"; } elsif ( ($type eq '2' )||( $type eq 'rectangle') ) { $type = "rectangle"; # etc. . . } elsif ( $type eq '' ) { last; } else { print "\n\n$help\n"; } } } print "Thank you for using my Calculator\n";
This is untested code, so I'll leave the debugging to you. I have introduced some new functions, but the idea is to build a framework for your future work. You could add a hash or array of types. You could make each call a separate subroutine, add HTML and this could be the start of a cgi script for your web server. But best of all you have a framework for you and your brother to control your environment with perl. Maybe your brother's class mates would like to use your program and expand on it. Put a copyright on it, and now you're an author.

Learning perl now, will enable you to do and control customized things for the rest of your life.

Personally, I like that you found a real problem (your brother's learning) and you are solving it yourself.

Good start!

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

Replies are listed 'Best First'.
Re^2: What is wrong with this code?
by perl.j (Pilgrim) on Jul 24, 2011 at 16:00 UTC

    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

      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