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

The purpose is that we are going to ask for two numbers. But if we type 'q' in the first number the program should quit with the msg "goodbye", otherwise if we type a number (negative or fraction) the program will ask for another number, and then the program will show the 4 math operations and the results of those 2 number, but if the second number happens to be zero, the program will skip the division operation.
#!/user/bin/perl -w use strict; my $quit = 0; print "Welcome to the mathq program\n"; #print "Enter a number\n"; #my $first_number = <STDIN>; #chomp ($first_number); until ($quit) { print "Enter a number\n"; my $first_number = <STDIN>; chomp ($first_number); if ($first_number eq 'q') { $quit = 1; die "Goodbye\n"; } else { print "Enter a second number\n"; my $second_number = <STDIN>; chomp ($second_number); my $add; my $subtract; my $times; my $divide; } if ($second_number != 0) { $add = $first_number + $second_number; $subtract = $first_number - $second_number; $times = $first_number * $second_number; $divide = $first_number / $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number x $second_number = $times\n"; print "$first_number / $second_number = $divide\n"; } elsif ($second_number == 0) { $add = $first_number + $second_number; $subtract = $first_number - $second_number; $times = $first_number * $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number * $second_number = $times\n"; } else { print "Invalid input: Type a number or 'q' to quit\n"; }

Replies are listed 'Best First'.
Re: need some help
by GrandFather (Saint) on Oct 24, 2007 at 08:49 UTC

    Well, that's a pretty ugly mess. ;)

    You gain points for use strict;, but lose more for awful indentation. And actually it is that awful indentation that is the biggest reason you are having trouble with the code. Consider this re-indented version (some lines omitted):

    #!/user/bin/perl -w use strict; my $quit = 0; print "Welcome to the mathq program\n"; until ($quit) { print "Enter a number\n"; my $first_number = <STDIN>; chomp ($first_number); if ($first_number eq 'q') { $quit = 1; } else { print "Enter a second number\n"; my $second_number = <STDIN>; ... } } if ($second_number != 0) { ... } elsif ($second_number == 0) { ... } else { print "Invalid input: Type a number or 'q' to quit\n"; }

    note that the calculation code it outside the loop! Note too that the variables that are used for the calculation step are declared inside a block inside the loop - actually that's where they should be except that the calculation code is nowhere in sight. So, a little restructuring:

    #!/user/bin/perl -w use strict; my $quit = 0; print "Welcome to the mathq program\n"; until ($quit) { print "Enter a number\n"; my $first_number = <STDIN>; chomp ($first_number); if ($first_number eq 'q') { $quit = 1; } else { print "Enter a second number\n"; my $second_number = <STDIN>; ... if ($second_number != 0) { ... } elsif ($second_number == 0) { ... } else { print "Invalid input: Type a number or 'q' to quit\n"; } } }

    fixes the primary issue, but still leaves room for a lot of tidying. For example, $quit is not needed if instead you use last to exit the loop.

    In general if you have code of the form:

    if (...) { ... last; } else { ... }

    you can restructure it as:

    if (...) { ... last; } ...

    If you have:

    if (X) { ... } elsif (! X) { ... } else { ... }

    there are two issues. The } elsif (! X) { can simply be } else {. But then it becomes obvious that the trailing else clause is bogus.

    Final hint: if you find yourself writing the same code in several places then you can refactor the code in some fashion to remove the duplication. In this case the calculation code contains two groups of three lines that are the same in both cases so they can all be moved outside the if and just the special case code needs to be managed by the if.


    Perl is environmentally friendly - it saves trees
Re: Homework: simple calculator
by blazar (Canon) on Oct 24, 2007 at 12:35 UTC

    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.

    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;
Re: Homework: simple calculator
by FunkyMonk (Bishop) on Oct 24, 2007 at 08:39 UTC
    It looks to me like you're very nearly there. All you seem to be missing is how to exit your loop when 'q' is pressed. last can help you with that.

Re: Homework: simple calculator
by m0ve (Scribe) on Oct 24, 2007 at 09:07 UTC
    i made some modifications to your code and it will now basically do what you need :
    #!/user/bin/perl -w use strict; use warnings; my ($add,$first_number,$second_number,$subtract,$times,$divide); print "Welcome to the mathq program\n\n"; while(1){ print "Enter a number : "; $first_number = <STDIN>; chomp($first_number); if ($first_number eq "q") { die "Goodbye\n"; } else { print "Enter a second number : "; $second_number = <STDIN>; print "\n\n"; chomp ($second_number); $add = $first_number + $second_number; $subtract = $first_number - $second_number; $times = $first_number * $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number x $second_number = $times\n"; if($second_number!=0){ $divide = $first_number / $second_number; print "$first_number / $second_number = $divide\n"; } print "\n\n"; } }

    but you'll still have to filter illegal inputs...

      If people here start about code layout, indentation, and useless constructs, I'd like to throw in one of my pet peeves here.

      if (expression) { die "reason"; } else { ... }

      is so unnessasary cluttering. A program flow stops after a die. There is no use for an else. Same after a return is a sub

      lc $first_number eq "q" and die "Goodbye\n"; ... rest of code

      Is so much easier to read and maintain. For those that like statement modifiers (I don't), you can also use

      die "Goodbye\n" if lc $first_number eq "q";

      Enjoy, Have FUN! H.Merijn
Re: Homework: simple calculator
by apl (Monsignor) on Oct 24, 2007 at 10:55 UTC
    You got some great help, and I hope you won't be a stranger here. But it helps us to help you if you tell us what your question/problem is. 8-)
Re: Homework: simple calculator
by R4j4 (Novice) on Oct 24, 2007 at 16:40 UTC
    Guys! I never thought that you ppl are that friendly and helpful. Thank you so much for your help guys. Your help is appreciated.
Re: Homework: simple calculator
by R4j4 (Novice) on Oct 25, 2007 at 01:33 UTC
    Hey guys, I've just modified the program. Thank you everyone for helping me and making sure I fully understand how to arrange my commands. Here is it after the modification. Hope anyone leave a comment whether the new program is good or not. Thanks again.
    #!/user/bin/perl -w use strict; use warnings; print "Welcome to the math program\n"; while (1) { print "Enter a number\n"; my $first_number = <STDIN>; chomp ($first_number); if ($first_number eq 'q') { die "Goodbye!\n"; } else { print "Enter a second number"; my $second_number = <STDIN>; chomp ($second_number); my $add; my $subtract; my $times; my $divide; if ($second_number !=0) { $add = $first_number + $second_number; $subtract = $first_number - $second_number; $times = $first_number * $second_number; $divide = $first_number / $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number x $second_number = $times\n"; print "$first_number / $second_number = $divide\n"; } else { $add = $first_number + $second_number; $subtract = $first_number - $second_number; $times = $first_number * $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number * $second_number = $times\n"; } } }

      Much better, but a little refactoring improves things even more:

      #!/user/bin/perl use strict; use warnings; print "Welcome to the math program\n"; while (1) { print "Enter a number (or q to quit)\n"; my $first_number = <STDIN>; chomp ($first_number); die "Goodbye!\n" if $first_number eq 'q'; print "Enter a second number"; my $second_number = <STDIN>; chomp ($second_number); my $add = $first_number + $second_number; my $subtract = $first_number - $second_number; my $times = $first_number * $second_number; print "$first_number + $second_number = $add\n"; print "$first_number - $second_number = $subtract\n"; print "$first_number x $second_number = $times\n"; if ($second_number !=0) { my $divide = $first_number / $second_number; print "$first_number / $second_number = $divide\n"; } }

      Of course there is another way to do it. Consider:

      #!/user/bin/perl use strict; use warnings; print "Welcome to the math program\n"; while (1) { print "Enter a number (or q to quit)\n"; my $first_number = <STDIN>; chomp ($first_number); die "Goodbye!\n" if $first_number eq 'q'; print "Enter a second number\n"; my $second_number = <STDIN>; chomp ($second_number); print "$first_number $_ $second_number = ", eval "$first_number $_ + $second_number", "\n" for $second_number != 0 ? qw(+ - * /) : qw(+ - *); }

      Perl is environmentally friendly - it saves trees