--- Kamali Muthukrishnan <[EMAIL PROTECTED]> wrote:
> 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.
Since your program is not actually using any of the CGI.pm functions, you don't need
to use this
module. The reason your script is having problems is *probably* because CGI.pm is
expecting some
sort of parameters on the command line or from STDIN when you run the program. I'd
have to look
into CGI.pm a bit deeper to understand why it appears to be causing this problem.
However, since
you're just running this from the command line and not from a Web page, I took the
liberty of
cleaning it up a bit. Hope this helps:
#!C:/Perl/bin/perl.exe
#Math.cgi - creates a dynamic Web page to do math
use strict;
#declare variables
my($correct,
$percent,
$totQuestions,
$totCorrect,
$totWrong,
$cont);
$totQuestions = 0;
$totCorrect = 0;
$totWrong = 0;
print "What grade are you in?(1 or 2): ";
chomp( my $grade = <STDIN>);
do
{
print "Would you like to do Addition or Subtraction?(A or S): ";
my $operation = lc substr <STDIN>,0,1;
my ( $num1, $num2, $answer ) = get_nums( $grade, $operation );
my $operator;
if( $operation eq 'a' )
{
$operator = '+';
}
else
{
$operator = '-';
}
print "$num1 $operator $num2 = ";
chomp ( my $student_answer = <STDIN> );
#checking for correct answer
if($answer == $student_answer)
{
print "Congratulations!! You are correct!\n\n";
$totCorrect += 1;
}
else
{
print "Your answer was: $student_answer\n";
print "$num1 $operator $num2 = $answer\n";
print "Sorry, that was incorrect.\n\n";
$totWrong += 1;
}
$totQuestions += 1;
print "Would you like to continue?(Y or N): ";
} while ( 'y' eq lc substr <STDIN>, 0, 1);
#Print totals and test percentage
print "Correct answers: $totCorrect\n";
print "Incorrect answers: $totWrong\n";
my $percent = ($totCorrect / $totQuestions) * 100;
printf "Test Percentage: %.2f%%\n", $percent;
sub get_nums
{
my ($grade, $operation) = @_;
my ($num1, $num2, $answer);
if($grade == 1)
{
( $num1, $num2 ) = ( 1+int rand(10), 1+int rand(10) );
}
else
{
( $num1, $num2 ) = ( 10+int rand(91), 10+int rand(100) );
}
if ( $operation eq 'a' )
{
$answer = $num1 + $num2;
}
else
{
$answer = $num1 - $num2;
}
return ( $num1, $num2, $answer );
}
Cheers,
Curtis "Ovid" Poe
=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]