PDA

View Full Version : Visual Basic Quiz Game problems, help!


owensoranges
01-08-2007, 09:10 PM
Hi, i'm very new to Visual Basic and am trying to make a simple True or False game about pythons for my youngest son who is fascinated by theml. I've managed to get quite far by using various tutorials but now have hit a problem: sometimes a question is repeated, and sometimes instead of True or False appearing, i'll get False and False or True and True. How do i remedy this?? Attached is the code. Hope someone can help! Thank you

Private Sub cmdNext_Click()
Dim VUsed(10) As Integer, I As Integer, J As Integer
Dim Index(2) As Integer
'Generate the next question
cmdNext.Enabled = False
Answer = Int(Rnd * 10) + 1
'Display selected question
lblQuestion.Caption = Question(Answer)
'Vused array to see which answers have been selected
For I = 1 To 10
VUsed(I) = 0
Next I
'Pick two different answer choices indices (J) at random
'Stored in the index array
For I = 1 To 2
'Find index not used yet and not the answer
Do
J = Int(Rnd * 10) + 1
Loop Until VUsed(J) = 0 And J <> Answer
VUsed(J) = 1
Index(I) = J
Next I
'replace one index (at random) with correct answer
Index(Int(Rnd * 2) + 1) = Answer
'Display multiple choice answers in label boxes
For I = 1 To 2
lblChoice(I).Caption = Choice(Index(I))
Next I
End Sub

Fatmumuhomer
01-09-2007, 04:08 PM
I think I know why you get the same question multiple times sometimes.

It looks like you are using an array of integers as flags to tell whether a particular question had been asked before. But your array that contains this information is local to the Click() function of your program. Whenever your program leaves this Click() function, that array will be lost because it is out of scope. So each time the Button is clicked, it's acting like the first time.

To solve this, you can either make the array global or have a hidden text field that contains the flags of which questions were asked. Then the info will be maintained from one click to the next.

If I'm mistaken, anyone is welcome to correct me. :)