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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.