The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pleas help..this is driving me crazy hckygoli31 Shell Programming and Scripting 0 05-13-2008 05:59 AM
Test-driving OpenOffice.org 3.0 iBot UNIX and Linux RSS News 0 04-04-2008 12:10 PM
Dumb question but its driving me nuts jepombar UNIX for Dummies Questions & Answers 3 07-03-2007 03:32 PM
Simple test driving me mad! alarmcall Shell Programming and Scripting 6 10-27-2003 10:29 AM
unix driving me crazy Tendernisin UNIX for Dummies Questions & Answers 1 11-29-2001 10:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-09-2009
daddygrant daddygrant is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 5
Short code driving me nuts.

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;
}


print 'The application is now terminated.';

###

Last edited by daddygrant; 02-09-2009 at 01:14 PM..
  #2 (permalink)  
Old 02-09-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,762
Code:
if ($answer eq "a" )
strings use eq or ne, etc for comparison
  #3 (permalink)  
Old 02-09-2009
daddygrant daddygrant is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 5
Same error on when i replace == with eq.
Quote:
if($calc == a)
  #4 (permalink)  
Old 02-09-2009
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
The string comparison would be for the y/n - not the mathematics
  #5 (permalink)  
Old 02-09-2009
daddygrant daddygrant is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 5
I change the values for comparison from string to integers but I still receive the same error. "Syntax error.. might be a runaway multi-line"

Code:
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 = 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 .";
        }
    print 'Would you like to perform another calculation? Please Type y or n';
    $run = <stdin>;
    chomp = $run;
    }


print 'The application is now terminated.';
  #6 (permalink)  
Old 02-09-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,871
This should work
Code:
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
  #7 (permalink)  
Old 02-09-2009
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0