in reply to having trouble with simple math game
Always use strict and warnings:
use strict; use warnings;
They are your friends:
> ./crop.pl Global symbol "$response1" requires explicit package name at ./crop.pl + line 41. Global symbol "$response1" requires explicit package name at ./crop.pl + line 48. Execution of ./crop.pl aborted due to compilation errors.
To get even that far I had to add some "my" statements:
#!/usr/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; if ($response1 eq "$correctanswer") { $cornplanted=($cornplanted + 1); print "correct!"; } elsif ($response1 ne "$correctanswer") { $cornnotplanted=($cornnotplanted + 1); print "not correct!"; }
You'll do much better to read the section CREATING FILL-OUT FORMS in the CGI.pm documentation and generate your form fields following proper CGI.pm practice.
To debug your script, follow the recommendations in the DEBUGGING section of the CGI.pm docs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: having trouble with simple math game
by Anonymous Monk on Nov 28, 2010 at 23:42 UTC | |
by mynameisG (Novice) on Nov 29, 2010 at 19:42 UTC |