rse2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello there amazing monks,

I'm having a bit of trouble here. Yesterday i turned in my Perl program assignment, and some one said there was something wrong with it.

My program was supposed to 1. ask a user to type in a number 2. ask user to type in another number. 3.asks user what is to be done add, subtract, divide, multiply. 4. display correct answer on screen.

#! /usr/bin/perl -w print "Type in a number: "; $x = <>; chop($x); print "Type in another number: "; $y = <>; chop($y); print "What would you like done?\n\nYour choice is:\n1) Add\n2) Subtra +ct\n3) Multiply\n4) Divide\n"; $ask = <>; chop ($ask); $z = $x + $y; if ($ask eq "Add") {print "$x + $y = $z";} $z = $x - $y; if ($ask eq "Subtract") { print "$x - $y = $z";} $z = $x * $y; if ($ask eq "Multiply") {print "$x * $y = $z";} $z = $x / $y; if ($ask eq "Divide") { print "$x / $y = $z";}

My teacher was unhappy about the $z = $x + $y, and all the variables like that.

May you please give me a bit of help in figuring this out? Can you please point out any mistakes you see?

Replies are listed 'Best First'.
Re: how to program my program to add random numbers?
by ww (Archbishop) on Aug 05, 2011 at 18:15 UTC
    Well, the obvious (and only reliable answer is "ask your teacher why s/he is 'unhappy about the variables" ( simple letter names? descriptive names are better).

    This is the 3rd thread you've begun with the same homework problem. And this time you've posed a question whose answer we can only guess at (you haven't identified your teacher, and even if you had, the Monastery's psi machine is down for an upgrade).

    At least as bad, this is basicly the same code you posted previously, minus the previous assignments re add, subtract, etc -- since those were useless, it's good that they're gone. But you're still choping data from the terminal, when you should be chomping it, despite very clear advice to use chomp in this thread and yesterday's ...and you need to read the docs so you understand the difference. See chop and chomp.

    Afterthought: numbering the choices when you don't intend to accept numeric replies is a bad idea: you may confuse the user -- "er, ah, um; should I enter '1' or 'add'?... which leads, if you think about it to a whole bunch of other improvements you could make: testing the input to see if it's numeric when numeric is appropriate; testing to see if it's alpha and one of the acceptable choices for the operation-to-perform input; maybe using a dispatch table so you can easily (and compactly) accept either '1' or 'a' or 'add' to determine the operation... and on and on....

    But first, you've got to pay attention to advice here; learn to search the documentation; and speak up when the teacher says something you don't understand.

Re: how to program my program to add random numbers?
by zek152 (Pilgrim) on Aug 05, 2011 at 15:47 UTC

    Can you be a little more clear about what the teacher was unhappy about?

    You don't have to have the $z = $x +|-|*|/ $y;. You can do the calculation in the print statement ({print "$x + $y = " . $x + $y . "\n";) and then you can remove the inefficient 3 checks (if the user entered "Add" it will still check to see if he entered sub,mult,div). Those should be elsifs.

    This is all speculation as I can't read your teacher's mind. Hope this helps a little.

Re: how to program my program to add random numbers?
by Marshall (Abbot) on Aug 05, 2011 at 16:11 UTC
    1. use chomp instead of chop
    2. don't calculate things that are not needed or used (get rid of statements before the prints).
    3. follow the spec: just print result., e.g. print $x+$y,"\n";
    4. if..elsif is fine to "shortcut" number of statements executed.
Re: how to program my program to add random numbers?
by JavaFan (Canon) on Aug 05, 2011 at 17:20 UTC
    As far as I see, there are no mistakes in that it produces what is asked. I wouldn't code it that way though. And I'd do some checking of the input. For instance, by using IO::Prompt:
    #!/usr/bin/perl use 5.010; use strict; use warnings; use IO::Prompt; my @operand; foreach my $word (qw[a another]) { push @operand, prompt -p => "Type in $word number: ", '-num'; } prompt -p => "What would you like done?", '-1', -menu => [qw[Add Subtract Multiply Divide]]; my $op = {qw[Add + Subtract - Multiply * Divide /]}->{$_}; say "$operand[0] $op $operand[1] = ", eval "$operand[0] $op $operand[1 +]"; __END__
    It still tries to divide by 0 if given the (un)appropriate input.