Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

calculator help

by Paradizingmania (Initiate)
on Jun 01, 2014 at 18:20 UTC ( [id://1088190]=perlquestion: print w/replies, xml ) Need Help??

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

hey guys i have just started learning perl, and i dont know much more than her (so dont use complicated answers or scripts) thanks in advance

#!/usr/bin/perl use strict; use warnings; print "please enter number 1 = "; my $num1 = <STDIN>; print "please enter operation = "; my $opr1 = <STDIN>; print "please enter number 2 = "; my $num2 = <STDIN>; if ($opr1 eq '+'){ print "$num1 $opr1 $num2 = " . ($num1 + $num2) . "\n"; } if ($opr1 eq '-'){ print "$num1 $opr1 $num2 = " . ($num1 - $num2) . "\n"; } if ($opr1 eq '*'){ print "$num1 $opr1 $num2 = " . ($num1 * $num2) . "\n"; } if ($opr1 eq '/'){ print "$num1 $opr1 $num2 = " . ($num1 / $num2) . "\n"; }

Replies are listed 'Best First'.
Re: calculator help
by Lady_Aleena (Priest) on Jun 01, 2014 at 20:13 UTC

    The first anonymous monk gave you some places to start reading, however, since it seems you need a quick answer, here it is.

    Before your if statements, add the following line to get rid of unwanted white space which would make your if statements return false (I hope I said that right)...

    chomp( $num1, $opt1, $num2 );
    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena

      also, to trim all leading/trailing white space:

      s/^\s+//; # trim leading white space s/\s+$//; # trim trailing white space

      Or use one of the Trim modules mentioned.

Re: calculator help
by Anonymous Monk on Jun 01, 2014 at 18:23 UTC
Re: calculator help
by Laurent_R (Canon) on Jun 01, 2014 at 22:35 UTC
    Lady_Aleena is (almost completely ) right: at the very least, anything that you get from the user using <STDIN> should be chomped to get rid of the end of line characters (the only very minor point where she is not completely right is that she has a small typo on the name of the variable, $opt1 instead of $opr1).

    This is especially important for the operation ($opr1 variable), that will never be equal to "+" (for example) if you don't chomp it, but more probably to things like "+\n" (under Unix) or "+\r\n" (under Windows) or something else (on Mac, I don't know for sure, it used to be "+\r", but I think it has changed some times ago and is probably like Unix nowadays). Anyway, whatever the trailing character(s) is not so important, the point is that you should remove it using the chomp function.

      Actually \n on all operating systems unless you do some work to make ity otherwise. Perl translates "local" line end conventions to \n during file I/O so you don't usually have to worry about it.

      Perl is the programming world's equivalent of English
        Thank you for the information, GrandFather, I did not know a conversion was done during I/O, I thought that it was chomp that was clever enough to remove "\n" or "\r\n" depending on whether the script was running on Unix or Windows, for example.

        We regularly have issues at work in a different type of situation, when we process files generated by another application running on another system, with end of lines sometimes converted and sometimes not converted during the transfer (FTP in ASCII or bin mode, SFTP, scp, etc.). We then need to pre-process the file before feeding them to our programs (in some cases we even have changed our programs so that they can handle smoothly both cases).

Re: calculator help
by AnomalousMonk (Archbishop) on Jun 02, 2014 at 00:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088190]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found