Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: having trouble with simple math game

by biohisham (Priest)
on Nov 26, 2010 at 02:52 UTC ( #873755=note: print w/replies, xml ) Need Help??

in reply to having trouble with simple math game

It seems to be reading the input as blank before the user even enters the answe
Your code is not giving the user a chance to enter the number that will be saved in $response1, then, your comparisons operators are those used for strings and not for numbers, and above all, you missed important warnings by not useing strict and warnings, here is your code retouched....
#!/usr/local/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; use CGI qw(:standard); my $cornplanted=0; my $cornnotplanted=0; #@number1=("12" .. "25"); #$number1rand=$number1[rand @number1]; my $number1rand=int(rand(20)); my $number2=int(rand(12)); my $correctanswer=($number1rand + $number2); print "$correctanswer <br>"; my $output=<<_html_; <html> <body> <form method="post" action="foodaddition.cgi" name="form2"> Crops planted: $cornplanted <br> <input type="hidden" name="$cornplanted" id="$cornplanted" /> Crops not planted: $cornnotplanted <br> <input type="hidden" name="$cornnotplanted" id="$cornnotplanted" /> What does this math problem equal? <br> $number1rand + $number2 = <input type="text" name="response1" id="resp +onse1" /> <input type="hidden" name="response1" id="response1" /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> _html_ print $output; chomp(my $response1 = <STDIN>); if ($response1 == $correctanswer) { $cornplanted=($cornplanted + 1); print "correct!"; } elsif ($response1 != $correctanswer) { $cornnotplanted=($cornnotplanted + 1); print "not correct!"; }
An interesting question, what if you wanted your program to respond to a non-conforming user entry (i.e when a user enters a sentence rather than a number)? Scalar::Util has an answer for you :)

Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

Replies are listed 'Best First'.
Re^2: having trouble with simple math game
by ysth (Canon) on Nov 26, 2010 at 03:02 UTC
Re^2: having trouble with simple math game
by mynameisG (Novice) on Nov 26, 2010 at 03:23 UTC

    Even with those changes it seems not be working correctly, when I hit the submit button, it still doesn't update the 'crops planted' and 'crops not planted'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://873755]
Eily sets the confetti thrower
[Discipulus]: Eily adjust the shot: i'll be at seaside when it will happen

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-07-27 10:06 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (409 votes). Check out past polls.