Procedures not working as they should?
Author |
Message |
yeyavailability
|
Posted: Fri May 30, 2008 7:42 pm Post subject: Procedures not working as they should? |
|
|
I'm making a quiz type of thing. I have a procedure that tells turing what the question number and its corresponding correct answer is.
Turing: |
var a, score : array 1..5 of int %'a' is number answer for questions
var total : int := 0
worda : array 1 .. 5 of string %Word answer for questions
procedure results (ans : string, num : int)
if a (num) = ans
then
score (num) := "correct."
total += 1
else
score (num) := "incorrect."
total += 0
end if
end results
results ("3",1)
results ("2",2)
results ("3",3)
results ("4",4)
results ("1",5)
|
Where a is the user input answer, num is the question number and ans is the correct answer to that question number. Score determines correctness or incorrectness.
At the end of the program, I put a results page that told the user whether or not he/she'd gotten the answer right, and if not what the right one is:
Turing: |
Font.Draw ("Question 1 | [Insert question here]", 15, 405, F_verdanaN, black)
Font.Draw ("You answered: ", 15, 390, F_verdanaN, black)
Font.Draw (a (1), 120, 390, F_verdanaN, black)
Font.Draw (worda (1), 132, 390, F_verdanaN, black)
Font.Draw ("Which was", 15, 375, F_verdanaN, black)
Font.Draw (score (1), 90, 375, F_verdanaN, black)
if score (1) = "incorrect."
then
Font.Draw ("The correct answer is: ", 15, 360, F_verdanaN, black)
Font.Draw ("[Correct answer]", 15, 345, F_verdanaN, black)
end if
|
For some reason, question five always ends up being right, no matter what answer I give to the prompt. All the rest are always incorrect--for example, if I answered question one "3", it would give me the text of the third answer and say that it's wrong while displaying the correct answer, which is exactly what it called incorrect. I can't figure out why it does this.
Once, I changed the ans part of the results procedure by subtracting two from all of the 'correct answers'. When I typed in the right answers, it displayed that all was correct.
Since that was confusing:
Turing: |
results ("1",1) %Correct answer: 3
results ("0",2) %Correct answer: 2 (-1 doesn't work so this ends up being wrong)
results ("1",3) %Correct answer: 3
results ("2",4) %Correct answer: 4
results ("1",5) %Correct answer: 1 (Can't do anything about that either)
|
And with that I'd put in the following: 3, 2, 3, 4, 1, and even though I'd coded the answers to be 1, 0, 1, 2, 1, everything (besides #2) would display that it is correct. As an edit, I tried going this again--for some reason, it doesn't work anymore.
I'm incredibly confused. Thankyou so much if you can help! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
gitoxa
|
Posted: Fri May 30, 2008 11:20 pm Post subject: RE:Procedures not working as they should? |
|
|
First of all, I don't know if it's because you posted snippets of code, but
Why are you comparing string and int variables?
Why are you assigning "(in)correct." to int variables?
What the hell does total += 0 mean? Nothing! So don't use it at all. If you want it there for visual aid, use comments. That's what they're for. |
|
|
|
|
|
Reality Check
|
Posted: Sat May 31, 2008 7:21 am Post subject: Re: Procedures not working as they should? |
|
|
Why is your score array of the integer type? |
|
|
|
|
|
yeyavailability
|
Posted: Sat May 31, 2008 4:38 pm Post subject: Re: Procedures not working as they should? |
|
|
Whoops. I don't know why that first block of code says int because it apparently says string now.
Nevertheless, it still doesn't work right.
I have to compare the string and int variables because I put 'a' and 'score' as an array, so the subscript must be int, and string for ans because 'a' was string.
I tried changing 'a' to int, but it still told me my answers were wrong. |
|
|
|
|
|
|
|