V
vkaar
Guest
here is a program I wrote. I would like to add a counter which at the end displays the total number of correct answers or percentage. Please help me with this
$^W = 1; # turn on warnings
use strict; # behave!
# display the startup message
print "Welcome to the mathq program.\n";
my $user_wants_to_quit = 0;
# LOOP until the user wants to quit
until ($user_wants_to_quit) {
# GENERATE A RANDOM MATH QUESTION AND SOLUTION=
# set first_num to a random integer in 0..9
my $first_num = int(rand(10));
# set second_num to a random integer in 0..9
my $second_num = int(rand(10));
my $question;
my $solution;
# pick a random integer from 0 to 1 and use it to choose operation
my $operator = int(rand(4));
if ( $operator == 0 ) {
# <<CREATE MULT QUESTION AND SOLUTION>>
$solution = $first_num * $second_num;
$question = "$first_num x $second_num = ";
}
if ( $operator == 1 ) {
# <<CREATE DIVISION QUESTION AND SOLUTION>>
$solution = $first_num * $second_num;
# swap values of first_num and solution
($solution, $first_num) = ($first_num, $solution);
$question = "$first_num / $second_num = ";
}
if ( $operator == 2) {
# <<CREATE SUBSTRACTION QUESTION AND SOLUTION >>
$solution = $first_num - $second_num;
$question = "$first_num - $second_num = ";
}
if ( $operator == 3) {
# <<CREATE ADDITION QUESTION AND SOLUTION>>
$solution = $first_num + $second_num;
$question = "$first_num + $second_num = ";
}
# <<DISPLAY QUESTION AND GET VALID RESPONSE>>=
my $response;
my $response_is_valid = 0; # set to FALSE
until ( $response_is_valid ) {
print "$question?\n> "; # display question
$response = <STDIN>;
chomp($response);
# <<CHECK IF RESPONSE IS VALID>>=
if ( $response =~ m/^[0-9]+$/ or $response eq 'q' ) {
$response_is_valid = 1;
}
}
# <<CHECK CORRECTNESS OF USER'S RESPONSE>>
if ( $response eq 'q' ) {
$user_wants_to_quit = 1;
}
else {
if ( $response == $solution ){
print "Correct!\n";
} else {
print "Incorrect: $question $solution \n";
}
}
}
# display the exit message
print "Exiting the mathq program.\n";
$^W = 1; # turn on warnings
use strict; # behave!
# display the startup message
print "Welcome to the mathq program.\n";
my $user_wants_to_quit = 0;
# LOOP until the user wants to quit
until ($user_wants_to_quit) {
# GENERATE A RANDOM MATH QUESTION AND SOLUTION=
# set first_num to a random integer in 0..9
my $first_num = int(rand(10));
# set second_num to a random integer in 0..9
my $second_num = int(rand(10));
my $question;
my $solution;
# pick a random integer from 0 to 1 and use it to choose operation
my $operator = int(rand(4));
if ( $operator == 0 ) {
# <<CREATE MULT QUESTION AND SOLUTION>>
$solution = $first_num * $second_num;
$question = "$first_num x $second_num = ";
}
if ( $operator == 1 ) {
# <<CREATE DIVISION QUESTION AND SOLUTION>>
$solution = $first_num * $second_num;
# swap values of first_num and solution
($solution, $first_num) = ($first_num, $solution);
$question = "$first_num / $second_num = ";
}
if ( $operator == 2) {
# <<CREATE SUBSTRACTION QUESTION AND SOLUTION >>
$solution = $first_num - $second_num;
$question = "$first_num - $second_num = ";
}
if ( $operator == 3) {
# <<CREATE ADDITION QUESTION AND SOLUTION>>
$solution = $first_num + $second_num;
$question = "$first_num + $second_num = ";
}
# <<DISPLAY QUESTION AND GET VALID RESPONSE>>=
my $response;
my $response_is_valid = 0; # set to FALSE
until ( $response_is_valid ) {
print "$question?\n> "; # display question
$response = <STDIN>;
chomp($response);
# <<CHECK IF RESPONSE IS VALID>>=
if ( $response =~ m/^[0-9]+$/ or $response eq 'q' ) {
$response_is_valid = 1;
}
}
# <<CHECK CORRECTNESS OF USER'S RESPONSE>>
if ( $response eq 'q' ) {
$user_wants_to_quit = 1;
}
else {
if ( $response == $solution ){
print "Correct!\n";
} else {
print "Incorrect: $question $solution \n";
}
}
}
# display the exit message
print "Exiting the mathq program.\n";