Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

my program will not display a correct answer, to a random question?

by rse2 (Initiate)
on Aug 04, 2011 at 16:35 UTC ( [id://918584]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to make my program 1. ask a user to type in a number 2. ask user to type in another number. 3.asks user what is to be done add, subtract, divide, multiply. 4. display correct answer on screen.

So far i got this done.

#! /usr/bin/perl -w $add = "Add"; $subtract = "Subtract"; $multiply = "Multiply"; $divide = "Divide"; print "Type in a number: "; $x = <>; chop($x); print "Type in another number: "; $y = <>; chop($y); print "What would you like done?\n\nYour choice is:\n1) Add\n2) Subtra +ct\n3) Multiply\n4) Divide\n"; $ask = <>; $z = $x + $y; if ($ask eq "Add") {print "$x + $y = $z";}

For know i'm trying to figure out the adding part.

When i perl it i get

Name "main::multiply" used only once: possible typo at rachael.pl line + 12. Name "main::add" used only once: possible typo at rachael.pl line 10. Name "main::divide" used only once: possible typo at rachael.pl line 1 +3. Name "main::subtract" used only once: possible typo at rachael.pl line + 11. Type in a number: 4 Type in another number: 5 What would you like done? you choice is: 1) Add 2) Subtract 3) Multiply 4) Divide 1 root:rachael {103}

can somebody please tell me what i could do to make it diplay the correct answer? Thanks

Replies are listed 'Best First'.
Re: my program will not display a correct answer, to a random question?
by toolic (Bishop) on Aug 04, 2011 at 16:50 UTC
Re: my program will not display a correct answer, to a random question?
by Anonymous Monk on Aug 04, 2011 at 16:47 UTC

    Try to ... chomp $ask;  if ($ask eq "1") .... Or enter Add instead of 1 (you still need to chomp($ask)).

    Some more hints:

    • use strict; (and add my to your variable declarations)
    • use chomp instead of chop
    • get rid of $z = $x + $y
    • introduce a dispatch-table
Re: my program will not display a correct answer, to a random question?
by Don Coyote (Hermit) on Aug 05, 2011 at 06:51 UTC

    Rachael,

    The error message shows that you have not made use of the scalars that you initialized at the top of the module. In the condition based on the return answer to the question, "Which operation would you like to undertake?" you have set the condition to compare the literal word "Add" rather than comparing it against the scalar $add which holds the string literal 'Add'. You could try either "$add" - or $add, as the scalar is interpolated (expanded to the value) within double quotes so they are equivalent.

    Also you may need to set up a condition so that it tests the operation input for either a '1' or the word 'Add' and then carries out the relevant operation.

    chomp($ask); # chomp() here rather than chop() if ($ask eq $add) { $z = $x + $y; print "$x + $y = $z";}

    or to start anticipating differing answer strings:

    chomp($ask); if ($ask eq $add || $ask == 1) { $z = $x + $y; print "$x + $y = $z";}

    you may like to think of prompting the participant again if they supply an invalid operation request, for example, if they spelt subtraqt incorrectly.

Re: my program will not display a correct answer, to a random question?
by ig (Vicar) on Aug 05, 2011 at 09:08 UTC

    I find it helpful to write programs that handle all possible input, processing expected input as required and giving error messages when unexpected input is received. In your case, you might have written your program as follows:

    use strict; use warnings; print "Type in a number: "; my $x = <>; chomp($x); die "ERROR: '$x' is not a number" unless($x =~ /^\d+$/); print "Type in another number: "; my $y = <>; chomp($y); die "ERROR: '$y' is not a number" unless($y =~ /^\d+$/); print "What would you like done?\n\nYour choice is:\n1) Add\n2) Subtra +ct\n3) Multiply\n4) Divide\n"; my $ask = <>; chomp($ask); if ($ask eq "Add") { my $z = $x + $y; print "$x + $y = $z"; } else { die "ERROR: '$ask' is an unknown operation"; }

    With the input you show in your post, this program gives:

    Type in a number: 4 Type in another number: 5 What would you like done? Your choice is: 1) Add 2) Subtract 3) Multiply 4) Divide 1 ERROR: '1' is an unknown operation at ./test.pl line 23, <> line 3.

    This may not be the "correct answer", but it may help you understand what is wrong.

Log In?
Username:
Password:

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

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

    No recent polls found