
-----------------------------------
dc116
Thu Mar 12, 2009 8:47 pm

Accumulators won't work
-----------------------------------
I'm making a quiz, in which it will only allow the user to have 3 guesses. If it took the user one guess to input the correct answer, the user will gain 3 marks. If it took the user two guesses to input the correct answer then the user will gain 2 marks. If the user took all 3 guesses to input the correct answer, he/she will only receive one mark. Otherwise, the user will receive no marks.

The problem is, when the user is doing the quiz, the accumulators (variables mark and correct) are not working. In the end, no matter how many questions you answered correctly, the report card will display your mark as 0.

You will see what I mean by running the program.


put "Problem Statement: To create a program which will display a menu with four"
put "choices. The user uses the keyboard to input his/her choice. The first"
put "choice is called 'Information' and contains a list of any ten countries from"
put "one continent along with their capital city. The second choice is the actual"
put "quiz and it asks the user for the capital of each country."
put "The user has three chances to answer the question. If the user gets it right"
put "on the first chance he/she, gets three points, on the second chance, two"
put "points and so on. The third choice shows the users score and percent out of "
put "the possible 30 points. The 4th choice is an exit, which will quit the program."
var x : string (1)
locate (25, 1)
put "Press any key to continue..." ..
getch (x)
cls

% DEFINE CONSTANT

% DECLARE VARIABLES
var choice : int
var y : string(1)
var answer : string
var mark : int := 0
var correct : int := 0
var font2,width2,font3,width3,width4,width5 : int
font2 := Font.New("Arial:24")
font3 := Font.New("Arial:14")
width2 := Font.Width("Report Card", font2)
width3 := Font.Width("Number of correct answers: ",font3)
width4 := Font.Width("Number of incorrect answers: ",font3)
width5 := Font.Width("Your final percentage: ",font3)
procedure ask (country, capital : string)
    var mark : int
    var correct : int
    var answer : string
    for i : 1..3
        put "What is the capital city of ", country, "?"
        get answer :*
        if answer = capital then
            exit
            mark += (4-i)
            correct += 1
        else put "Sorry, try again!"
        end if
    end for
    cls
end ask

loop
    setscreen("text")
    %display menu
    put "" : 38, "MENU"
    put skip
    put "" : 20, "1.  Information"
    put "" : 20, "2.  Quiz"
    put "" : 20, "3.  Results"
    put "" : 20, "4.  Exit"
    % missed lines
    get choice
    %checks for the range - ADD THE CODE TO CHECK THE INPUT FROM THE KEYBOARD
    %clear screen where is needed etc.
    case choice of
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %displays information
        label 1 :
        setscreen("graphics")
        cls
        put "Please study for the quiz. All the possible questions are given below."
        put "You have 2 seconds to read each hint."
        put "Press any key to continue..." ..
        getch(y)
        cls
        put "The capital city of Austria is Vienna."
        delay(2000)
        cls
        put "The capital city of Denmark is Copenhagen."
        delay(2000)
        cls
        put "The capital city of France is Paris."
        delay(2000)
        cls
        put "The capital city of Germany is Berlin."
        delay(2000)
        cls
        put "The capital city of Greece is Athens."
        delay(2000)
        cls        
        put "The Capital City of Italy is Rome."
        delay(2000)
        cls        
        put "The Capital City of Russia is Moscow."
        delay(2000)
        cls        
        put "The Capital City of Spain is Madrid."
        delay(2000)
        cls        
        put "The Capital City of Sweden is Stockholm."
        delay(2000)
        cls        
        put "The Capital City of United Kingdom is London."
        delay(2000)
        cls
        put "We hope you have finished studying."
        put "Press any key to go back to the main menu."
        getch(y)
        cls
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %ask questions, assign score, accumulate total
        label 2 :
        setscreen("graphics")
        cls
        ask("France", "Paris")
        ask("United Kingdom", "London")
        ask("Spain", "Madrid")
        ask("Italy", "Rome")
        ask("Sweden", "Stockholm")
        ask("Greece", "Athens")
        ask("Austria", "Vienna")
        ask("Denmark", "Copenhagen")
        ask("German", "Berlin")
        ask("Russia", "Moscow")
        put "You have finished the quiz."
        put "Press any key to go back to the main menu."
        getch(y)
        cls
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        % display point score and percentage score
        label 3 :
        setscreen("Graphics:400;500")        
        cls
        Draw.Box(50,50,350,450,7)
        Draw.Line(50,400,350,400,7)
        Font.Draw("Report Card",200-(width2 div 2),410,font2,7)
        Font.Draw("Number of correct answers: ",60,370,font3,7)
        Font.Draw("Number of incorrect answers: ",60,340,font3,7)
        Font.Draw(intstr(correct),60+width3,370,font3,7)
        Font.Draw(intstr(30-correct),60+width4,340,font3,7)
        Font.Draw("Your final percentage: ",60,310,font3,7)
        Font.Draw(realstr(mark/30*100,3),60+width5,310,font3,7)
        Draw.FillBox(120,200,140,280,7)
        Draw.FillBox(260,200,280,280,7)
        if mark > 20 then
            Draw.FillBox(90,80,110,150,7)
            Draw.FillBox(290,80,310,150,7)
            Draw.FillBox(110,80,290,100,7)
        elsif mark > 10 and mark 