I've written a perl script that is supposed to create a guessing game on the web

  • Thread starter Thread starter shannon w
  • Start date Start date
S

shannon w

Guest
I could use some help? HERE IS MY CODE

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

use CGI qw/:standard/;

print_header;
print start_html('Guessing Game'),
start_form,
"Guess a Number!",textfield('guess'),
p,
submit,
end_form,
hr;

$guesses = 0;


$a = int(rand(100))+1;

while ()
{
print "\nEnter a number from 1 to 100: ";
$guess = <STDIN>;
chomp $guess;

$guesses++;

if ($guess == $a)
{
print "Random Number: $a\n";
print "Number of Guesses: $guesses\n";
next;
}
elsif ($guess > $a)
{
print "Too high\n";
next;
}
else
{
print "Too low\n";
last;
}
}

print end_html;
The page shows up but the form doesn't work it prints out too low, and doesn't let any numbers be submitted...
 
The issue is that you are not getting the guess from the web form. You have coded as if the user is interacting directly with the script, which is not the case. The interaction is happening through the web page, so when the user submits the form, the script has to get the user input from the form. I'm including a link to a tutorial for cgi programming in Perl.
 
Back
Top