Problem with a string


D

Default

Hi im new to perl and programming, could you please help me with this problem?
When I enter two out of range values I get unexpected results.
eg: -20 and 20
gives this for output: Negative plus equals zero

Here is the program (sorry for my noobness).
Thank you for any help.
Sorry if this is a double post, im new to posting nntp also : |

#!
hash_one ();
print "\n" . "="x80;
print "Takes two numeric values from -9 to 9 adds them then returns the English names.";
print "\n" . "="x80 . "\n\n";
until (($key1 eq "end") || ($key2 eq "end"))
{
$neg1 = ""; # Im doing this to try to fix the problem
$neg2 = ""; #
$neg3 = ""; # o 0
$key1 = ""; # _
$key2 = ""; #
$key3 = ""; #
print "Enter a number (0-9):\t\t";
chomp ($key1 = <STDIN>);
print "Enter another number (0-9):\t";
chomp ($key2 = <STDIN>);
$key3 = ($key1 + $key2);
if ($key1 < 0)
{
$neg1 = "negative ";
$key1 = (- $key1);
}
if ($key2 < 0)
{
$neg2 = "negative ";
$key2 = (- $key2);
}
if ($key3 < 0)
{
$neg3 = "negative ";
$key3 = (- $key3);
}
lookup_one ($key3);
}
#####
sub lookup_one
{
if (($key1 eq "end") || ($key2 eq "end"))
{
print "\nEnding program\n";
last
}
if ($hash_one{$key3})
{
$output1 = $neg1 . $hash_one{$key1} . " plus " . $neg2 . $hash_one{$key2} . " equals " . $neg3 . $hash_one{$key3};
print "\n\u$output1\n\n";
}
else
{
$output2 = $neg1 . $key1 . " plus " . $neg2 . $key2 . " equals " . $neg3 . $key3;
print "\n\u$output2\n\n";
}

}
sub hash_one
{
%hash_one = qw(
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
12 twelve
13 thirteen
14 fourteen
15 fifteen
16 sixteen
17 seventeen
18 eighteen
19 nineteen);
}
 
Ad

Advertisements

G

Gunnar Hjalmarsson

When I enter two out of range values I get unexpected results.
eg: -20 and 20
gives this for output: Negative plus equals zero

Unexpected? Why? What had you expected it to print?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top