Solved

need some help getting this simple perl program to run

Posted on 2009-05-12
5
973 Views
Last Modified: 2012-05-06
print "Rey Calculator";
print "\n";
print "Please Enter the first Number";
chomp($a = <STDIN>);
print "\n";
print "Please Enter the second Number\n";
chomp ($b = <STDIN>);
print "\n";
print "Please Enter the Operator"
chomp ($operation = <STDIN>);
print "\n";
#The following if statement assigns the command line arguments to a variable ($operation)
if ($operation eq +) {
$result = $a + $b;
}elsif ($operation eq -) {
$result = $a - $b;
}elsif ($operation eq /) {
$result = $a / $b;
}elsif ($operation eq x) {
$result = $a * $b;
}
print $a $operation $b = $result\n;


C:\>perl cal.pl
syntax error at cal.pl line 10, near "chomp"
Unrecognized character \x93 in column 19 at cal.pl line 13
0
Comment
Question by:1030071002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 40 total points
ID: 24371589
missing ; between
print "Please Enter the Operator"  
and
chomp ($operation = <STDIN>);
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 40 total points
ID: 24371594
And what is the character between the eq and the +
if ($operation eq +) {
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 20 total points
ID: 24371643
This appears to be homework... is it?  If so, experts can help point you in the right direction, but can not do the work for you.
0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 65 total points
ID: 24371658
There are two problems in the above script:
* First is a missing semicolon at the end of line no. 9 (print "Please Enter the Operator")
* Second is you need to put all the operators in the if statements in quotes. like '+'
So your line no. 13 now becomes:
if ($operation eq '+') {

This is because you are comparing $operation as a text value.
I have debugged the code tried running it and is working fine now.
Find the corrected script below:

print "Rey Calculator";
print "\n";
print "Please Enter the first Number";
chomp($a = <STDIN>);
print "\n";
print "Please Enter the second Number\n";
chomp ($b = <STDIN>);
print "\n";
print "Please Enter the Operator";
chomp ($operation = <STDIN>);
print "\n";
#The following if statement assigns the command line arguments to a variable ($operation)
if ($operation eq '+') {
$result = $a + $b;
}elsif ($operation eq '-') {
$result = $a - $b;
}elsif ($operation eq '/') {
$result = $a / $b;
}elsif ($operation eq x'') {
$result = $a * $b;
}
print $a $operation $b = $result\n;

Open in new window

0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 65 total points
ID: 24371675
One small in copy paste above.On line 20 by mistake have type x'', it should be 'x'
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question