in reply to help me to find out the error??
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.
|
|---|