Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help writing program for True/False quiz
#1
I am writing a QBasic program for a true/false quiz for an assignment for an Intro to Computers class. I have the program written, the trouble I am having is with getting the program to "keep score". At the end of the quiz it's supposed to display the score and I am stumped as to how to count the number of correct responses. Thanks for any help you can give!
Reply
#2
post your code.

EDIT:
or

whenever you get it right

numright = numright + 1

and at the end

print numright
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
You could do something like this, assuming you capture the answer from a list of possible answers as 1, 2, 3, 4, 5, 6, for example, in the variable Answer, the correct answer number has been made equal to the variable Correct, and the variable Score will keep track of the number of correct results:
Code:
IF Answer = Correct THEN Score = Score + 1
Then, at the end, assuming you have made the variable Total equal to the total number of questions in your test, you could enter something like:
Code:
PRINT "Great! You have answered";Score;"questions correctly, out of a total of";Total;", or";100*Score/Total;"%"
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#4
DECLARE SUB Subgrade ()
DECLARE SUB SubScore ()
DECLARE SUB SubProverbs ()
REM Proverbs Quiz
CLS
PRINT "Evaluate the following proverbs as true or false.";
DIM num(1 TO 7)
DIM proverb$(1 TO 7)
DIM answer$(1 TO 7)
CALL SubProverbs
DATA 1, "The squeaky wheel gets the grease.","true"
DATA 2, "Cry and you cry alone.", "true"
DATA 3, "Opposites attract.", "false"
DATA 4, "Spare the rod and spoil the child.", "false"
DATA 5, "Actions speak louder than words.", "true"
DATA 6, "Familiarity breeds comtempt.", "false"
DATA 7, "Marry in haste, repent at leisure.", "true"

SUB Subgrade
tally = 0
IF word$ = answer$ THEN
tally = tally + 1
END IF
END SUB

SUB SubProverbs
FOR num = 1 TO 7
READ num, proverb$, answer$
PRINT proverb$
INPUT "Please enter true or false:"; word$
IF word$ = answer$ THEN
PRINT "correct";
PRINT
ELSE
PRINT "wrong";
PRINT
END IF
CALL Subgrade
NEXT num
RESTORE
CALL SubScore
END SUB

SUB SubScore
IF tally = 7 THEN
PRINT "Perfect"
END IF
IF tally <= 6 THEN
PRINT "Excellent"
END IF
IF tally <= 4 THEN
PRINT "You might consider Psychology 101."
END IF

END SUB
Reply
#5
uhhh isn't that a bit overdoing it? You don't even need SUBs.
but if you want to you need to set tally as SHARED

put this in the main code before you access the SUBs
Code:
DIM SHARED tally AS LONG
(i think that's it. Doing it from memory)




woah that post was hard to make.... That's what I get for talking to someone in 1337 for a half an hour over IM...
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#6
to write a program using subprograms. At the end of the quiz I need to display the number correct along with one of the evaluation statements. i.e. 7 correct Perfect
Reply
#7
In programs, when you go into a different subprogram, even if the variable has the same name, qb treats it as a different variable. As a result when you call the subgrade, answer$ and word$ will be empty strings, and so will tally. The remedy is just before you call subproverbs add this line:
Code:
dim shared tally, word$, answer$
That way the variables are shared by all subprograms.

Also in the subgrade program every time it is callled tally is reset to 0(at the top is the line tally = 0). Remove that line. Finally the subscore subprogram. If you get all correct it displays "Perfect". However if tally is under 4 there is a problem. Any number under 4 is also under 6 so it displays both "excellent" and "You might consider Psychology 101" messages. The remedy is to replace
Code:
If tally=< 6 then
with
If tally =<6 and tally>4 then

Here's the modified code that works if my explanation made no sense

Code:
DECLARE SUB Subgrade ()
DECLARE SUB SubScore ()
DECLARE SUB SubProverbs ()
REM Proverbs Quiz
CLS
PRINT "Evaluate the following proverbs as true or false.";
DIM num(1 TO 7)
DIM proverb$(1 TO 7)
DIM answer$(1 TO 7)
DIM SHARED tally, word$, answer$
CALL SubProverbs
DATA 1, "The squeaky wheel gets the grease.","true"
DATA 2, "Cry and you cry alone.", "true"
DATA 3, "Opposites attract.", "false"
DATA 4, "Spare the rod and spoil the child.", "false"
DATA 5, "Actions speak louder than words.", "true"
DATA 6, "Familiarity breeds comtempt.", "false"
DATA 7, "Marry in haste, repent at leisure.", "true"

SUB Subgrade
'tally = 0
IF word$ = answer$ THEN
tally = tally + 1
END IF
END SUB

SUB SubProverbs
FOR num = 1 TO 7
READ num, proverb$, answer$
PRINT proverb$
INPUT "Please enter true or false:"; word$
IF word$ = answer$ THEN
PRINT "correct";
PRINT
ELSE
PRINT "wrong";
PRINT
END IF
CALL Subgrade
NEXT num
RESTORE
CALL SubScore
END SUB

SUB SubScore
PRINT "You got" + STR$(tally) + " correct"
IF tally = 7 THEN
PRINT "Perfect"
END IF
IF tally <= 6 AND tally > 4 THEN
PRINT "Excellent"
END IF
IF tally <= 4 THEN
PRINT "You might consider Psychology 101."
END IF
END SUB

Any original code was commented out.
am part of the legion of n00b. We are numerous if dumb. We will enslave you all!
Reply
#8
Thank you so much for your help! Now tonight I can finally get some sleep!
Reply
#9
Also, why did you include a number in the DATA statments?

Code:
[...]
FOR num = 1 TO 7
READ num, [...]

bit pointless?

EDIT: Was about to say.....
"Nuuuuuuuu you gave him code!"
but was to lazy...
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#10
Hey I did explain why I added or removed the code.......
am part of the legion of n00b. We are numerous if dumb. We will enslave you all!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)