Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

negative numeric strings with STDIN

by DarknessX (Scribe)
on May 17, 2003 at 19:52 UTC ( #258893=perlquestion: print w/replies, xml ) Need Help??

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

After numerous supersearches, I still couldn't come up with an answer to my question. I'm in precalc, so to make life easy I wrote up a quick little quadratic equation solver. My question lies not in that however, but rather in why I'm having issues with numeric and nonnumeric strings. my code is quite simple,
print "A?="; chomp(my $a=<STDIN>); print "B?="; chomp(my $b=<STDIN>); #get Ax**2+Bx+C values print "C?="; chomp(my $c=<STDIN>); my $ans1=(($b x -1)+sqrt(($b**2) - 4 x $a x $c)) / (2 x $a) or die "Un +real answer!\n"; #note the ugliness of ($b x -1), any better way to g +et -$b? my $ans2=(($b x -1)-sqrt(($b**2) - 4 x $a x $c)) / (2 x $a); print "Answers: $ans1 AND $ans2\n\n";
With warnings, I get errors which look like Argument "" isn't numeric in addition (+) at ./quad.pl line 11, <STDIN> line 3.. Without warnings, of course I don't get these. So, being a competent perl programmer, I want to use warnings, or figure out why I can't. How do I get numeric strings from STDIN?

Replies are listed 'Best First'.
Re: negative numeric strings with STDIN
by The Mad Hatter (Priest) on May 17, 2003 at 20:02 UTC
    Well, the main problem I see is that you are using x instead of * for multiplication. x is the string repeat operator. For example
    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:

    #!/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; }
    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.

    And as for a more elegant way of making a number negative, simply do -$b.

Re: negative numeric strings with STDIN
by chromatic (Archbishop) on May 17, 2003 at 20:04 UTC

    The x operator isn't multiplication. You mean to use the * operator. See perlop for more. (-1 instances of any string is going to be empty, at best)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://258893]
Approved by jonnyfolk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2021-12-05 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?