shrsv:

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.


In reply to Re: Find the biggest number from three numbers by roboticus
in thread Find the biggest number from three numbers by shrsv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.