in reply to Find the biggest number from three numbers
I'm in a rambling mood right now, so this'll be a bit long.
First, I'd suggest that you pay a little more attention to the formatting of your code. I've reformatted it in two ways. First I put a blank line between the input section, the processing section and the output section to make the boundaries between them a bit more obvious. I also indented the nested code blocks:
#!/usr/bin/perl print "Enter a number\n"; chomp($a=<stdin>); print "Enter second number\n"; chomp($b=<stdin>); print "Enter third number\n"; chomp($c=<stdin>); $big=0; $equal=0; if($a eq $b){ $big = $a; $equal = $a; } elsif($a > $b){ $big=$a; } else{ $big = $b; } if($equal eq $c){ + print "All numbers are same"; } elsif($big < $c){ $big = $c; } else{ print "The biggest number is $big \n"; }
Having done so, it's easier to see at least one of the errors: In your reporting section, one of your if branches doesn't print anything, so if you entered 1, 2, 3 as your numbers, then $big would be less than $c, and nothing would be printed:
$ perl 878546.pl Enter a number 1 Enter second number 2 Enter third number 3 $
The way I'd suggest repairing it would be to move the middle case into the processing section, so you complete all your processing before reporting anything:
#!/usr/bin/perl print "Enter a number\n"; chomp($a=<stdin>); print "Enter second number\n"; chomp($b=<stdin>); print "Enter third number\n"; chomp($c=<stdin>); $big=0; $equal=0; if($a eq $b){ $big = $a; $equal = $a; } elsif($a > $b){ $big=$a; } else{ $big = $b; } if ($big < $c) { $big = $c; } if($equal eq $c){ print "All numbers are same"; } else{ + print "The biggest number is $big \n"; }
The next immediately apparent error is that you're trying to use $equal for two purposes, which is often a bit tricky. For example, check out this test case:
$ perl 878546.pl Enter a number 1 Enter second number 2 Enter third number 0 All numbers are same
The problem is that you're trying to use $equal to be a flag to tell when the numbers are equal and to hold the value. But since you can enter any value for $c, you can get stuck. So I'd suggest using $equal as a simple yes/no flag. You can use $big to hold the value when they're equal, since if the values are equal, then $big will hold the correct value anyway. So to fix this bug, I'd change the code to something like:
#!/usr/bin/perl print "Enter a number\n"; chomp($a=<stdin>); print "Enter second number\n"; chomp($b=<stdin>); print "Enter third number\n"; chomp($c=<stdin>); $big=0; $equal=0; + if($a eq $b){ $big = $a; $equal = 1; } elsif($a > $b){ $big=$a; } else{ $big = $b; } if ($big < $c) { $big = $c; } if ($big != $c) { $equal = 0; } if($equal == 1){ print "All numbers are same"; } else{ print "The biggest number is $big \n"; }
Here's a test case that'll show you another error, but I'll leave the solution to you, as I don't want to spoil the fun. Enter " 1", "1" and "1 " (without the quotes--they're just there to show you which blanks you'll want to enter), and you'll see that the program won't tell you that the numbers are all the same.
Finally, I'll make a suggestion: Make your program take an arbitrary number of input values.
Have fun!
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the biggest number from three numbers
by shrsv (Novice) on Dec 23, 2010 at 01:43 UTC |