in reply to negative numeric strings with STDIN
print 3 x 4; # This prints "3333" print 3 * 4; # This prints 12
Added Here is a fixed and cleaned up version of your code:
The major thing I added was the prompt sub. It will prompt the user for a value, and if the value isn't a real number, it will continue to prompt them until they enter a real number.#!/usr/bin/perl -w use strict; use Regexp::Common; my $a = prompt("A? = "); my $b = prompt("B? = "); my $c = prompt("C? = "); my $root = sqrt(($b**2) - 4 * $a * $c) / (2 * $a); my $ans1 = -$b + $root; my $ans2 = -$b - $root; print "Answers: $ans1 AND $ans2\n\n"; sub prompt { my $var; do { print $_[0]; $var = <STDIN>; chomp $var; } while ($var !~ /\A$RE{num}{real}\z/); return $var; }
And as for a more elegant way of making a number negative, simply do -$b.
|
|---|