Short code driving me nuts.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Short code driving me nuts.
# 1  
Old 02-09-2009
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  
Old 02-09-2009
Code:
if ($answer eq "a" )

strings use eq or ne, etc for comparison
# 3  
Old 02-09-2009
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!
# 4  
Old 02-09-2009
Same error on when i replace == with eq.
Quote:
if($calc == a)
# 5  
Old 02-09-2009
The string comparison would be for the y/n - not the mathematics
# 6  
Old 02-09-2009
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.';

# 7  
Old 02-09-2009
correct this:

Code:
elsif$4calc == 6)

to:

Code:
elsif ($calc == 6)

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

This is driving me nuts error on line 17

There is an error in this script on line 17 bee at it for 12 hour trying to find the problem, just lost. please help a newbie. line 17: #!/bin/sh 2 ## Name of the program is test_script_b.sh 3 4 ##request information from the user 5 echo enter 3 numbers 6 read a b c 7 8... (4 Replies)
Discussion started by: osdustin
4 Replies

2. Hardware

Name These Little Nuts for my Server Cabinet?

I bought a server cabinet recently and the person who sold it to me included some strange nuts. Can anyone tell me what they are called? I need to buy more. Below is the image of them. Thank you! http://s23.postimg.org/50fx2ge6j/image.jpg http://s23.postimg.org/o498isr0r/image.jpg ... (1 Reply)
Discussion started by: danijeljames
1 Replies

3. Homework & Coursework Questions

Abnormal producer consumer problem driving me nuts

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: normally, i hate asking someone to do my homework for me but am getting desperate right now. i have a project... (1 Reply)
Discussion started by: alexantosh
1 Replies

4. UNIX for Dummies Questions & Answers

Abnormal producer consumer problem driving me nuts

normally, i hate asking someone to do my homework for me but am getting desperate right now. i have a project about consumer producer problem. the deadline is tonight at 23:55. but i havent gotten it working yet. i just COULDNT get it to work right yet. the problem is as follows: the C - program... (0 Replies)
Discussion started by: alexantosh
0 Replies

5. UNIX for Dummies Questions & Answers

Variable with @ sign is driving me crazy

Ok so I am working on a command that is going to do a Dig @ a certain IP address which is enclosed in a variable. Now I thought I had this figure out because it works. The problem is that it does not ALWAYS work. the variable is IP=192.168.1.1 the commands I have tried are dig... (2 Replies)
Discussion started by: MrEddy
2 Replies

6. Shell Programming and Scripting

Escape Characters are driving me crazy!

Hi everyone, Is there anywhere I can find a complete table of all characters that must be escaped by the various UNIX shells and scripting languages? It seems every command/shell/scripting language has different rules about what characters must be escaped. I do a lot of searching and... (3 Replies)
Discussion started by: troym72
3 Replies

7. Shell Programming and Scripting

Pleas help..this is driving me crazy

Hi, I've created a script in csh that takes a file and checks it for mispelled words. Im almost done but I need to do two more things but I need help. First, when displaying an incorrect word to the user, I need to show the line of the input file that contains the word. Second,if the user... (0 Replies)
Discussion started by: hckygoli31
0 Replies

8. UNIX for Dummies Questions & Answers

Dumb question but its driving me nuts

I use a tcsh and I need to monitor a file that its constantly updating. In the past I remember using less -(an option) fileName. This option told less to keep listening to this file and output the any changes. What is this option??? I think I read the man page for less like 5 times and I... (3 Replies)
Discussion started by: jepombar
3 Replies

9. UNIX for Dummies Questions & Answers

unix driving me crazy

:( :confused: what is performed by the following unix command: grep -v Jane project1.txt and grep ' 5\..' janet.txt (1 Reply)
Discussion started by: Tendernisin
1 Replies
Login or Register to Ask a Question