Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to program a simple quiz maker. I have run into some questions regarding the output. The fifth question does not print. Also, can anyone give me ideas on how I should go about using an if/then statement to grade the test. Thanks. Attached is the code I have thus far:
#!/usr/bin/perl use strict; use CGI qw(:standard); print header, start_html("Quiz Maker"), h1("Quiz Maker"); if (param()) { my $who = param("firstname"); my $who2 = param("lastname"); my $email = param("email"); my $firstquestion = param("FirstQuestion"); my $secondquestion = param("SecondQuestion"); my $thirdquestion = param("ThirdQuestion"); my $fourthquestion = param("FourthQuestion"); my $fifthquestion = param("FirsthQuestion"); my $answerfirstquestion = param("AnswerFirstQuestion"); my $answersecondquestion = param("AnswerSecondQuestion"); my $answerthirdquestion = param("AnswerThirdQuestion"); my $answerfourthquestion = param("AnswerFourthQuestion"); my $answerfifthquestion = param("AnswerFifthQuestion"); my $useranswerfirstquestion = param("UserAnswerFirstQuestion"); my $useranswersecondquestion = param("UserAnswerSecondQuestion"); my $useranswerthirdquestion = param("UserAnswerThirdQuestion"); my $useranswerfourthquestion = param("UserAnswerFourthQuestion"); my $useranswerfifthquestion = param("UserAnswerFifthQuestion"); my $choicesfirstquestion = param("ChoicesFirstQuestion"); my $choicessecondquestion = param("ChoicesSecondQuestion"); my $choicesthirdquestion = param("ChoicesThirdQuestion"); my $choicesfourthquestion = param("ChoicesFourthQuestion"); my $choicesfifthquestion = param("ChoicesFifthQuestion"); print p("Thank You. Here is your quiz:"); print p("Name: $who $who2"); print hr(), start_form(); print p("Question #1: $firstquestion"); print p("Answer:", textfield("UserAnswerFirstQuestion")); print hr(), start_form(); print p("Question #2: $secondquestion"); print p("Answer:", textfield("UserAnswerSecondQuestion")); print hr(), start_form(); print p("Question #3: $thirdquestion"); print p("Answer:", textfield("UserAnswerThirdQuestion")); print hr(), start_form(); print p("Question #4: $fourthquestion"); print p("Answer:", textfield("UserAnswerFourthQuestion")); print hr(), start_form(); print p("Question #5: $fifthquestion"); print p("Answer:", textfield("UserAnswerFifthQuestion")); } else { print hr(), start_form(); print p("What's your first name?", textfield("firstname")); print p("What's your last name?", textfield("lastname")); print p("Enter your e- mail address", textfield("email")); print hr(), start_form(); print p("What is your first question", textfield("FirstQuestion")) +; print p("Please enter the answer", textfield("AnswerFirstQuestion" +)); print hr(), start_form(); print p("What is your second question", textfield("SecondQuestion" +)); print p("Please enter the answer", textfield("AnswerSecondQuestion +")); print hr(), start_form(); print p("What is your third question", textfield("ThirdQuestion")) +; print p("Please enter the answer", textfield("AnswerThirdQuestion" +)); print hr(), start_form(); print p("What is your fourth question", textfield("FourthQuestion" +)); print p("Please enter the answer", textfield("AnswerFourthQuestion +")); print hr(), start_form(); print p("What is your fifth question", textfield("FifthQuestion")) +; print p("Please enter the answer", textfield("AnswerFifthQuestion" +)); print hr(), start_form(); print p(submit("Submit"), reset("Reset")); print end_form(), hr(); } print end_html

Edit by tye, add READMORE

Replies are listed 'Best First'.
Re: Simple Quiz Maker
by davido (Cardinal) on Dec 05, 2003 at 04:05 UTC
    Here's your most immediate problem:

    my $fifthquestion = param("FirsthQuestion");

    I would recommend less cutting/pasting, and more Perlish idioms, as they tend to reduce the probability of this type of error. The problem is that to learn the "Perl Way" (which isn't just a single way), is to learn Perl. But as you learn Perl, your scripting will mature, and you'll be able to accomplish more.

    A great place to start is Learning Perl, an O'Reilly & Associates book. :) And of course hanging out here awhile helps too. It's not as hard as it might seem at first, and on the other hand, it's a quest never completed. *smile*

    Good luck!


    Dave

Re: Simple Quiz Maker
by Roger (Parson) on Dec 05, 2003 at 06:30 UTC
    Make a list of which (first ...) and WHICH (First ...) so that you generate the CGI parameter names on the fly. Store questions and answers in arrays. I have rewritten part of your code to show you how this is done.
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); my @which = qw/ first second third fourth fifth /; my @WHICH = qw/ First Second Third Fourth Fifth /; print header, start_html("Quiz Maker"), h1("Quiz Maker"); if (param()) { my $who = param("firstname"); my $who2 = param("lastname"); my $email = param("email"); my @questions = map { param("$WHICH[$_]Question") } 0..4; my @answers = map { param("Answer$WHICH[$_]Question") } 0..4; #my @user_answers = ... # you get the picture #my @choices = ... print p("Thank You. Here is your quiz:"); print p("Name: $who $who2"); for (0..4) { print hr(), start_form(); print p("Question #". $_+1 .": $questions[$_]"); print p("Answer:", textfield("UserAnswer$WHICH[$_]Question")); } } else { print hr(), start_form(); print p("What's your first name?", textfield("firstname")); print p("What's your last name?", textfield("lastname")); print p("Enter your e- mail address", textfield("email")); for (0..4) { print hr(), start_form(); print p("What is your $which[$_] question", textfield("$WHICH[ +$_]Question")); print p("Please enter the answer", textfield("Answer$WHICH[$_] +Question")); } print hr(), start_form(); print p(submit("Submit"), reset("Reset")); print end_form(), hr(); } print end_html;
Re: Simple Quiz Maker
by duff (Parson) on Dec 05, 2003 at 04:52 UTC
Re: Simple Quiz Maker
by Anonymous Monk on Dec 05, 2003 at 04:11 UTC
    Thanks for the help. Can anyone give me ideas on how to use an if/else statement to correct the quiz?
      I ran your program and it seems that Roger has answered your if/else statement question, if that's what you meant.

      Your other problem is a typo which could have been averted using the Vars() method provided by CGI.pm - here's how you'd do that:
      my $q = CGI->new; my $form_variables = $q->Vars; print $form_variables->{FirstQuestion}; # etc...
      I'd have a read through the CGI.pm manpage if I were you.

      As a rule, if you find yourself copy-and-pasting lumps of code inline you can be certain that you are doing something wrong. In this example, your program would be improved by generalising the repeated print statements into a subroutine that took as its arguments a question/answer pair.

      As a general observation, the HTML you are outputting isn't up to much either. Consider closing tags on all those forms at least.

      MB
Re: Simple Quiz Maker
by Anonymous Monk on Dec 05, 2003 at 08:42 UTC
    Another problem I have run into is that the textfields do not show up (lines 37, 41, 45, 49, 53). Any help would be greatly appreciated. Thanks.
      Lots of start_forms but only one end_form? The start/end form tags should be matched, ie remove all but the first start_form.

      rdfield