First of all, ++ for having specified that this is homework in the first place, and for showing what you have tried thus far. Said this, I personally believe that your program is more complex than it ought to be. For example the following may suffice:
#!/user/bin/perl use strict; use warnings; print "Welcome to the mathq program\n"; while (1) { print "Enter a number\n"; chomp(my $n = <STDIN>); die "Goodbye\n" if $n eq 'q'; chomp(my $m = <STDIN>); print "$n + $m = ", $n+$m, "\n", "$n - $m = ", $n-$m, "\n", "$n x $m = ", $n*$m, "\n", $m == 0 ? '' : ("$n / $m = ", $n/$m), "\n"; } __END__
Please note that a value in Perl is either ==0 or !=0 -possibly passing through a complaint if warnings are on- and thus you "validation" for input is not going to work. You may want to add your own validation, e.g.
#!/user/bin/perl use strict; use warnings; print "Welcome to the mathq program\n"; INPUT: while (1) { print "Enter a number\n"; chomp(my $n = <STDIN>); die "Goodbye\n" if $n eq 'q'; chomp(my $m = <STDIN>); for ($n,$m) { warn "Invalid input: Type a number or 'q' to quit\n" and next INPUT unless /\A\d+\z/; # use your own more refined validation there } print "$n + $m = ", $n+$m, "\n", "$n - $m = ", $n-$m, "\n", "$n x $m = ", $n*$m, "\n", $m == 0 ? '' : ("$n / $m = ", $n/$m), "\n"; } __END__
Or at least force $n and $m to their numeric values, to avoid printing strange things:
($n,$m) = map { ;no warnings 'numeric'; $_+0; } $n, $m;
In reply to Re: Homework: simple calculator
by blazar
in thread Homework: simple calculator
by R4j4
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |