Hello guys I am trying to learn perl and have a simple calculator I am trying to run but I get error runaway multi-line. Can someone point this rookie in the right direction.
###
print 'Welcome to the Calculator';
print 'Would you like to enter the calculator? Please Type y or n';
$run = <stdin>;
chomp $run;
while ($run == y)
{
{
print 'Add = a | Subtract = s | Multiply = m | Divide = d |Exponent = e | Mod = O ';
print 'Enter the corresponding number to the caluclation to be performed:';
$calc = <stdin>;
chomp $calc;
print 'Please enter the first value: ';
$A = <stdin>;
chomp $A;
print 'Please enter the second value: ';
$B = <stdin>;
chomp $B;
if\($calc == a\)
\{
$answer = $A \+ $B;
\}
elsif\($calc == s\)
\{
$answer = $A - $B;
\}
elsif\($calc == m\)
\{
$answer = $A * $B;
\}
elsif\($calc == d\)
\{
$answer = $A / $B;
\}
elsif\($calc == e\)
\{
$answer = $A ** $B;
\}
elsif\($calc == o\)
\{
$answer = $A % $B;
\}
print "The answer is $answer .";
\}
print 'Would you like to perform another calculation? Please Type y or n';
$run = <stdin>;
chomp = $run;
\}
I'd recommend that you create a subroutine with your calculator functions inside.
Have your script ask if the user wants to enter the calculator - call the subroutine.
Have your script ask if the user wants to continue - if yes, call the subroutine. if no, exit.
Edit: I was looking at format not syntax... bad Avron!
print 'Welcome to the Calculator';
print 'Would you like to enter the calculator? Please Type y or n';
$run = <stdin>;
chomp $run;
while ($run eq "y")
{
# Not needed {
print 'Add = 1 | Subtract = 2 | Multiply = 3 | Divide = 4 |Exponent = 5 | Mod = 6 ';
print 'Enter the corresponding number to the caluclation to be performed:';
$calc = <stdin>;
chomp $calc;
print 'Please enter the first value: ';
$A = <stdin>;
chomp $A;
print 'Please enter the second value: ';
$B = <stdin>;
chomp $B;
if($calc == 1)
{
$answer = $A + $B;
}
elsif($calc == 2)
{
$answer = $A - $B;
}
elsif($calc == 3)
{
$answer = $A * $B;
}
elsif($calc == 4)
{
$answer = $A / $B;
}
elsif($calc == 5)
{
$answer = $A ** $B;
}
elsif$4calc == 6)
{
$answer = $A % $B;
}
print "The answer is $answer .";
# Not needed }
print 'Would you like to perform another calculation? Please Type y or n';
$run = <stdin>;
chomp $run;
}
print 'The application is now terminated.';
Notes for the future
Always start your Perl code with use warnings; use strict; This can help you catch a lot of bugs before they become bugs.
Please post more of the error output next time (eg "(Might be a runaway multi-line )) string starting on line 22)" instead of "Might be a runaway multi-line"
If you want a String in Perl, use " or ' to enclose, and eq to test them
[+] print does not append a newline to it's output, you'll have to add that yourself
Everything is functioning now. Next I'll try to convert it into subroutines like another poster suggested. I have to do some reading up.
#####################
print 'Welcome to the Calculator.';
print "\n\n";
print 'Would you like to enter the calculator? Please Type y or n: ';
$run = <stdin>;
chomp $run;
# FIXED THE PROBLEM while ($run eq "y")
{
{
print "Add = 1 | Subtract = 2 | Multiply = 3 | Divide = 4 |Exponent = 5 | Mod = 6 \n";
print 'Enter the corresponding number to the caluclation to be performed:';
$calc = <stdin>;
chomp $calc;
print 'Please enter the first value: ';
$A = <stdin>;
chomp $A;
print 'Please enter the second value: ';
$B = <stdin>;
chomp $B;
if\($calc == 1\)
\{
$answer = $A \+ $B;
\}
elsif\($calc == 2\)
\{
$answer = $A - $B;
\}
elsif\($calc == 3\)
\{
$answer = $A * $B;
\}
elsif\($calc == 4\)
\{
$answer = $A / $B;
\}
elsif\($calc == 5\)
\{
$answer = $A ** $B;
\}
elsif\($calc == 6\)
\{
$answer = $A % $B;
\}
print "The answer is $answer .\\n";
\}
print 'Would you like to perform another calculation? Please Type y or n: ';
$run = <stdin>;
#FIXED THE PROBLEM chomp $run;
}
print 'The application is now terminated.';
[/CODE]