Solved

need some help getting this simple perl program to run

Posted on 2009-05-12
5
964 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now