dacka has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl print"###simple calculator program##\n\n"; until ($opt2 eq'y') { print "enter the first number\n"; $num1=<>; print "enter the second number\n"; $num2=<>; print "enter your option\n\+ for addition\n\- for subtraction\n\* for +multiplication\n\/ for divison\n option:"; $opt=<>; chomp($opt); if ($opt eq'+') { print "you pressed the \+key\n press any key to continue\n"; <>; $ans=$num1+$num2; print "the answer is $ans\n"; } elsif ($opt eq'-') { print "you pressed the \-key\n press any key to continue\n"; <>; $ans=$num1-$num2; print "the answer is $ans\n"; } elsif ($opt eq'*') { print "you pressed the \*key\n press any key to continue\n"; <>; $ans=$num1*$num2; print "the answer is $ans\n"; } elsif ($opt eq'/') { print "you pressed the \/key\n press any key to continue\n"; <>; $ans=$num1/$num2; print "the answer is $ans\n"; }else { print "you have entered wrong option\n"; } print " do you want to continue press y or n?\n"; $opt2=<>; }else { print "thanks for using\n"; } <>;

Replies are listed 'Best First'.
Re: help me to find out the error??
by marto (Cardinal) on Apr 11, 2014 at 14:06 UTC

    Here is your example code with a few changes, I'm really pushed for time, this is by no means perfect, just something to help you get started:

    #!/usr/bin/perl use strict; use warnings; print"###simple calculator program##\n\n"; my $opt2=""; while ($opt2 ne 'y'){ my $ans; print "enter the first number\n"; chomp( my $num1 = <> ); print "enter the second number\n"; chomp( my $num2 = <> ); print "enter your option\n\+ for addition\n\- for subtraction\n\* +for multiplication\n\/ for divison\n option:"; chomp(my $opt = <>); if ($opt eq '+'){ print "you pressed the \+key\n"; $ans = $num1 + $num2; print "the answer is $ans\n"; }elsif ($opt eq '-'){ print "you pressed the \-key\n"; $ans = $num1 - $num2; print "the answer is $ans\n"; }elsif ($opt eq '*'){ print "you pressed the \*key\n"; $ans = $num1 * $num2; print "the answer is $ans\n"; }elsif ($opt eq '/'){ print "you pressed the \/key\n"; $ans = $num1 / $num2; print "the answer is $ans\n"; }else{ print "you have entered wrong option\n"; print "do you want to continue press y or n?\n"; chomp ($opt2 =<>); } }

    Firstly fix your indentation, making things easier to read can help you find out what's wrong. I removed the press any key to contiune type thing, it felt pointless.

    You have lots ot elsifs but two elses for the same if condition, this is going to cause problems.

Re: help me to find out the error??
by Laurent_R (Canon) on Apr 11, 2014 at 18:56 UTC
    This is a somewhat shorter version, using a dispatch table (drawing some bits and pieces from your and marto's code).
    #!/usr/bin/perl use strict; use warnings; print"###simple calculator program##\n\n"; my $opt2 = "y"; my %operations = ( "+" => sub { return $_[0] + $_[1]}, "-" => sub { return $_[0] - $_[1]}, "*" => sub { return $_[0] * $_[1]}, "/" => sub { return $_[0] / $_[1]} ); while ($opt2 eq "y"){ print "enter the first number\n"; chomp( my $num1 = <> ); print "enter the second number\n"; chomp( my $num2 = <> ); print "enter your option\n\+ for addition\n\- for subtraction\n\* +for multiplication\n\/ for divison\n option:"; chomp(my $opt = <>); print "you have entered wrong option\n" and next unless defined $o +perations{$opt}; print "you pressed the $opt key\n"; print $operations{$opt}->( $num1, $num2), "\n\n"; print "do you want to continue press y or n?\n"; chomp ($opt2 =<>); }
    The good thing about this technique is that if you want to add an operation, say exponentiation, you only need to add one single line of code in the dispatch table (well, plus one line in your menu option). In addition, the return statement is not needed in the subs defined in the dispatch table (a function returns the last evaluated expression), so that the dispatch table could be:
    my %operations = ( "+" => sub { $_[0] + $_[1]}, "-" => sub { $_[0] - $_[1]}, "*" => sub { $_[0] * $_[1]}, "/" => sub { $_[0] / $_[1]}, "**" => sub { $_[0] ** $_[1]} );
    .
Re: help me to find out the error??
by LanX (Saint) on Apr 11, 2014 at 13:46 UTC
    which error?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      until.pl line 45,near "}else" its showing like aborted due to compilation errors
        and what is  until ( ) {...} else {...} supposed to mean?

        Perl ne Python ;-)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        until is a looping construct. else is part of an if() statement. They don't link together as you're trying to do.


        Dave