Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Debug basic calculator syntax

by ROB6789 (Novice)
on Jun 23, 2005 at 20:56 UTC ( [id://469511]=perlquestion: print w/replies, xml ) Need Help??

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

HI, I'm Trying to make a "calculator" that can do addition and subtraction... but i can't get it to work. Here's the code:

print "Would You like to Add Two numbers? y/n \n"; chomp($add = <STDIN>); if ($add = "y") { print "OK, press enter to continue \n"; chomp($nothing = <STDIN>); print "Enter Your First Number \n"; chomp($first = <STDIN>); print "Enter Your Second Number \n"; chomp($second = <STDIN>); $result = $first + $second; print "$first + $second = $result.\n"; chomp($end = <STDIN>) } else { print "Would You Like to Multiply Two Numbers? y/n \n"; chomp($mult = <STDIN>); if ($mult = "y") { print "Enter Your First Number \n"; chomp($firstm = <STDIN>); print "Enter Your Second Number \n"; chomp($secondm = <STDIN>); $result = $firstm * $secondm; print "$firstm * $secondm = $result.\n"; chomp($end2 = <STDIN>) } else { print "cool"; } chomp($jjf = <STDIN>); }

ayudame porfavor!

janitored by ybiC: Retitle from less-than-descriptive "What's The problem?" for better site search results

Replies are listed 'Best First'.
Re: Debug basic calculator syntax
by kirbyk (Friar) on Jun 23, 2005 at 21:07 UTC
    The first thing that jumps out is the '=' inside the if clauses. You really want to use 'eq' for string comparisons and == for number comparisons. What you're doing is assigning 'y' to $add.

    It also might help if you said what happened when you tried to run it. And include the !# line, so we can tell you to add a -w to it, so perl will give you extra helpful hints.

    In general, this is going to raise people's "Asking for help with homework" flag, so you'll get more help if you show what you've tried to do to debug it, and maybe why you think the line that's broken should work. People like to educate those striving to learn, while are annoyed at giving people answers if they just want to avoid learning the language in the first place.

    -- Kirby, WhitePages.com

Re: Debug basic calculator syntax
by CountZero (Bishop) on Jun 23, 2005 at 21:08 UTC
    The comparison operator is not '=' but 'eq' if you want to compare strings. If you change them, the program works.

    Update: If you would have activated "warnings" Perl would have told you 'Found = in conditional, should be == at line 19.' which is helpful but misleading as you really should use 'eq' for string comparison.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Ah...but the second run after changing '=' to '==' will give this error: 'Argument "foo" isn't numeric in numeric eq (==) at line <script name> <line number>'. It's the old theory of debugging; fix exactly one error at a time, because each error that you fix could be either creating or masking other errors.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        Well observed! That's why I said the warning message was helpful but misleading.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Debug basic calculator syntax
by blue_cowdawg (Monsignor) on Jun 23, 2005 at 21:04 UTC

    My litiany of counter-questions in a case like this one:

    • What happens when you run this?
    • What errors (if any) are you seeing?

    use strict; use warnings; use diagnostics;
    is your friend...

      when i run the program, it will only do the adding part of the program reguardless of if i enter "n"...or anything else

        perldoc perlop
        is your other friend... you have lots of friends in Perl.. Kinda reminds me of the old Sunday school song (with apologies to nobody) "What a friend we have in Camel!"

        Anyway, take a look at your comparison statement in your first "if" statement. You are doing an assign and not a comparison.

        Check out perlop... you'll be glad you did...

Re: Debug basic calculator syntax
by GrandFather (Saint) on Jun 23, 2005 at 21:56 UTC

    Many others have answered your immediate question. I note that your code actually would do multiplication and addition.

    You may be interested in another way of doing the calculator thing. You may be interested in adding some more operators to this example.


    Perl is Huffman encoded by design.
      Although the issue at hand might be to correct the above example code, forgive me if I mention the obvious - i.e. that for evaluating formulas read in as data, the eval function does seem rather suitable. Because it is functionally overkill, a filter function is also supplied in this quickie example:

      #!/usr/bin/perl do { print "CALC> "; printf "%f\n", eval( Filter(<>) ); } until 0; sub Filter { my $exp = shift; ( $exp =~ /^\s*exit/ ) and return $exp; ( $exp =~ /^\s*(\w+)/ ) and return 'print STDERR "$1 denied\n"'; return $exp; }

      example session:

      CALC> 0.7*0.9

      0.630000

      CALC> 0.6-0.8

      -0.200000

      CALC> mkdir freddy;

      mkdir denied

      CALC> exit

      $

      -S

Re: Debug basic calculator syntax
by TheStudent (Scribe) on Jun 23, 2005 at 21:13 UTC
    You should always use
    use strict; use warnings;
    It would have shown you immediately you have a problem with:
    if ($add = "y") and if ($mult = "y")

    Rather than using '=' it should be '=='.

    TheStudent

      ++ for advising to use strict and warnings; -- for saying to use '==' for string comparisons (it should be 'eq').

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        I jumped to reply without thinking. The warnings from perl said:
        Found = in conditional, should be == at calc.pl Found = in conditional, should be == at calc.pl

        TheStudent

      I believe you mean, "Rather than using '=' it should be 'eq'."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found