Hi :
I have the following perl-script , which gives the user ( supposedly a first or second
grader) a problem in addition or subtraction by generating random numbers. The random
numbers generated are between 1 and 10 for first graders and 10 and 100 for second
graders. I am trying to just execute the code at the command prompt. It runs OK if
the line -
use CGI qw(:standard );
is commented out. The loop goes through till I say 'n' to the query - do you want to
continue ?
If that line is not commented out ( I know it is not needed till I put a HTML piece to
this )
it does not give an error, but runs wrong. Even if I pick 'Addition' as the operation
I want, it gives me the subtraction problem (that is even if the expression in the if
statement is true, it executes the 'else' ) and the loop does not go on till I say
'n'. Can somebody tell me why ?( I am a beginner)
#!C:/Perl/bin/perl.exe
#Math.cgi - creates a dynamic Web page to do math
#use CGI qw(:standard );
use strict;
#declare variables
my($grade,
$operation,
$num1,
$num2,
$answer,
$correct,
$percent,
$totQuestions,
$totCorrect,
$totWrong,
$cont);
$percent = 0;
$totQuestions = 0;
$totCorrect = 0;
$totWrong = 0;
print "What grade are you in?(1 or 2): ";
chomp($grade = <STDIN>);
do
{
print "Would you like to do Addition or Subtraction?(A or S): ";
chomp($operation = <STDIN>);
if($operation eq 'A' || $operation eq 'a')
{
if($grade == 1)
{
$num1 = 1+int(rand(10));
$num2 = 1+int(rand(10));
print "$num1 + $num2 = ";
chomp($answer = <STDIN>);
}
else
{
$num1 = 10+int(rand(91));
$num2 = 10+int(rand(100));
print "$num1 + $num2 = ";
chomp($answer = <STDIN>);
}
}
else
{
if($grade == 1)
{
$num1 = 1+int(rand(10));
$num2 = 1+int(rand(10));
print "$num1 - $num2 = ";
chomp($answer = <STDIN>);
}
else
{
$num1 = 10+int(rand(91));
$num2 = 10+int(rand(91));
print "$num1 - $num2 = ";
chomp($answer = <STDIN>);
}
}
#checking for correct answer
if($operation eq 'A' || $operation eq 'a')
{
if($answer == ($num1 + $num2))
{
print "$num1 + $num2 = $answer\n";
print "Congratulations!! You are correct!\n";
$totCorrect += 1;
}
else
{
$correct = $num1 + $num2;
print "$num1 + $num2 = $correct\n";
print "Your answer was: $answer\n";
print "Sorry, that was incorrect.\n";
$totWrong += 1;
}
$totQuestions += 1;
}
else
{
if($answer == ($num1 - $num2))
{
print "$num1 - $num2 = $answer\n";
print "Congratulations!! You are correct!\n";
$totCorrect += 1;
}
else
{
$correct = $num1 - $num2;
print "$num1 - $num2 = $correct\n";
print "Your answer was: $answer\n";
print "Sorry, that was incorrect.\n";
$totWrong += 1;
}
$totQuestions += 1;
}
print "Would you like to continue?(Y or N): ";
chomp($cont = <STDIN>);
}while($cont eq 'Y' || $cont eq 'y');
#Print totals and test percentage
print "Correct answers: $totCorrect\n";
print "Incorrect answers: $totWrong\n";
$percent = ($totCorrect / $totQuestions) * 100;
printf "Test Percentage: %.2f%%\n", $percent;
Thanks
Kamali
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]