Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Accumulators won't work
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dc116




PostPosted: Thu Mar 12, 2009 8:47 pm   Post subject: 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.

Turing:

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 <= 20 then
            Draw.FillBox(90,100,310,120,7)
        else
            Draw.FillBox(90,80,110,150,7)
            Draw.FillBox(290,80,310,150,7)
            Draw.FillBox(110,130,290,150,7)
        end if
        delay(5000)
        cls
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%       
        label 4 :
            exit
    end case
end loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% good bye screen and stop execution of your program
setscreen("graphics")
cls
var font,width : int
font := Font.New("Arial:26")
width := Font.Width("Thank you for using this program! =)", font)
Font.Draw("Thank you for using this program! =)", maxx div 2 - width div 2, 300, font, red)



Any help would be greatly appreciated.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Mar 12, 2009 8:53 pm   Post subject: RE:Accumulators won\'t work

are you familiar with scope?

http://en.wikipedia.org/wiki/Scope_(programming)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Thu Mar 12, 2009 8:58 pm   Post subject: RE:Accumulators won\'t work

No I really have no idea what scope is. How does scope relate to my program?
Tony




PostPosted: Thu Mar 12, 2009 9:03 pm   Post subject: RE:Accumulators won\'t work

you have a problem with it.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Thu Mar 12, 2009 9:28 pm   Post subject: RE:Accumulators won\'t work

Can you give me more hints? because I have no idea what scope is in turing (there are no such examples on wikipedia)
Tony




PostPosted: Thu Mar 12, 2009 9:44 pm   Post subject: RE:Accumulators won\'t work

it works the same way.

Now that I've actually tried to run your program, Turing actually gives a warning before executing. Looks like there are more problems in there.

Either way, keep track of how the value of mark variable changes, as the program is running.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Thu Mar 12, 2009 9:56 pm   Post subject: Re: Accumulators won't work

Is it because that every time the "ask" procedure is used, another "mark" variable is declared?
Tony




PostPosted: Thu Mar 12, 2009 10:05 pm   Post subject: RE:Accumulators won\'t work

that is a partial answer. What else can you say about the "mark" variable?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
dc116




PostPosted: Thu Mar 12, 2009 10:08 pm   Post subject: RE:Accumulators won\'t work

Perhaps it is used improperly? E.g. accumulators are not supposed to be used within procedures?
Tony




PostPosted: Thu Mar 12, 2009 10:20 pm   Post subject: RE:Accumulators won\'t work

what value is it initialized to? How (and under what conditions) does it increment?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Thu Mar 12, 2009 10:24 pm   Post subject: RE:Accumulators won\'t work

Well, in the beginning I set "mark" to zero.
For each "ask" procedure, mark increases from between 0-3 depending on the correctness of the answer.

So is my procedure done improperly?
Tony




PostPosted: Thu Mar 12, 2009 10:30 pm   Post subject: Re: RE:Accumulators won\'t work

dc116 @ Thu Mar 12, 2009 10:24 pm wrote:
mark increases from between 0-3

which "mark"?
dc116 @ Thu Mar 12, 2009 9:56 pm wrote:
Is it because that every time the "ask" procedure is used, another "mark" variable is declared?


also the increase depends on more than just the correctness of the answer. What conditions must be true for that line to execute?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Thu Mar 12, 2009 10:38 pm   Post subject: RE:Accumulators won\'t work

Ohhhh I see. Since mark += (4-i) is the same as mark := mark + (4-i), but every time the ask procedure is used, another mark variable is declared and the previous values all disappear. Is that somewhat along the lines of my problem?
Tony




PostPosted: Thu Mar 12, 2009 10:41 pm   Post subject: RE:Accumulators won\'t work

Yes, you are correct.

Though you have another problem as well -- your program never gets to the mark += (4-i) line; there's something that makes it skip a chunk of code, and force the flow of execution to go elsewhere.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Fri Mar 13, 2009 3:43 pm   Post subject: RE:Accumulators won\'t work

So any ideas on how to use an accumulator to actually add the marks properly? How do I accumulate outside a procedure?

EDIT: Never mind... I figured out that the "exit" has to go after the "mark += (4-i)",
Turing:

procedure ask (country, capital : string, var mark, correct : int)
    var answer : string
    for i : 1..3
        put "What is the capital city of ", country, "?"
        get answer :*
        if answer = capital then
            mark += (4-i)
            correct += 1
            exit
        else put "Sorry, try again!"
        end if
    end for
    cls
end ask


... and also that I have to declare the variables "mark" and "correct" in a different way

Turing:

procedure ask (country, capital : string, var mark, correct : int)


Thanks for all those who helped. Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: