Qbasicnews.com
March 30, 2020, 10:41:38 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Need help writing program for True/False quiz  (Read 2013 times)
iteachmykids
New Member

Posts: 4


« on: January 17, 2005, 11:45:06 PM »

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!
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #1 on: January 17, 2005, 11:52:18 PM »

post your code.

EDIT:
or

whenever you get it right

numright = numright + 1

and at the end

print numright
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Ralph
Ancient Guru
****
Posts: 544


« Reply #2 on: January 17, 2005, 11:58:16 PM »

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;"%"
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
iteachmykids
New Member

Posts: 4


« Reply #3 on: January 18, 2005, 12:07:34 AM »

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
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #4 on: January 18, 2005, 12:42:04 AM »

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...
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
iteachmykids
New Member

Posts: 4


« Reply #5 on: January 18, 2005, 12:45:41 AM »

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
Logged
SBM Productions
Senior Member
**
Posts: 163


« Reply #6 on: January 18, 2005, 01:03:18 AM »

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.
Logged

 am part of the legion of n00b. We are numerous if dumb. We will enslave you all!
iteachmykids
New Member

Posts: 4


« Reply #7 on: January 18, 2005, 01:18:00 AM »

Thank you so much for your help! Now tonight I can finally get some sleep!
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #8 on: January 18, 2005, 01:19:31 AM »

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...
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
SBM Productions
Senior Member
**
Posts: 163


« Reply #9 on: January 18, 2005, 01:36:50 AM »

Hey I did explain why I added or removed the code.......
Logged

 am part of the legion of n00b. We are numerous if dumb. We will enslave you all!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!