Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is a nicely-written program, well done! If the C++ version is up to this standard, you should get high marks on the assignment.

I did notice one logical error:

print "Division by zero not allowed, starting over..." and redo if ( $frac01 =~ /^[\s\S]?\/?0$/ );

If the user enters 1/0, this correctly flags division by zero. But,

  • If the user enters 12/0, the regex fails and division by zero is not caught.
  • If the user enters 10, the regex matches and gives a false positive.

How to fix this? One way would be to extract the denominator (if any) and test whether that is zero. But this is Perl, so TMTOWTDI.

Now to style: In Perl, it’s better to declare a variable as late as possible in the code. So, looking at this line:

my ( $f1, $f2, $frac01, $frac02, $command, $total ) = (0) x 6;

actually only $command needs to be declared before the outer while loop. The rest can safely be declared at the point of first use. (And yes, I would declare $total separately within each of the if / elsif blocks.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Overloaded operators calling similar functions... by Athanasius
in thread Overloaded operators calling similar functions... by dbuckhal

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found