Short code driving me nuts.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Short code driving me nuts.
# 8  
Old 02-09-2009
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
# 9  
Old 02-09-2009
Thanks for the advice. Let me give it a swing.
# 10  
Old 02-09-2009
Sweet

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]

Thank You everyone.
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