Why won't this work?

I Karma I
02-09-2008, 05:06 PM
I am just messing around with textboxes and whatnot, here is my code:
Dim total As Integer

Private Sub clear_Click()
Cls
End Sub

Private Sub Cmd_add_Click()

If textbox.Text = 5 Then
Cls
Print "You got it!"
Print "Type in 15 for your next test!"
Else
Print "Guess again! +1 to total"
total = total + 1
End If

If total >= 5 Then
Print "You failed!"
End If

If textbox.Text = 15 Then
total = 0
Cls
Print "1234568 - whats missing?"
End If

If textbox.Text = 7 Then
Cls
Print "you finished!"
Else
Print "Guess again!"
total = total + 1
End If
End Sub




Now when I type 5, it says I got it correct, but it ALSO says guess again down the bottom..

I then type 15, and it says the whats missing part AND guess again..

I then type 7 and it says You finished.

Whats wrong with the code?

Qua
02-09-2008, 06:17 PM
You are testing for all 3 values at the same time. You need first to test only for 5, then test for 15 and then at last for 7.

I Karma I
02-09-2008, 06:53 PM
So how would I go about testing individually?

passel
02-09-2008, 09:25 PM
Rethink your approach.
Don't have them type a number into the textbox to go to the next question.
When the question is answered correctly, move on to the next question. If the number of guesses is exceeded, then fail and exit the test.

Use a local variable, make it static, to keep track of which question you're on, that way you only process the code for that question, and not all the questions.

Logically, something like this (pseudo code, not real code).


Static CurrentPhase as Long

If CurrentPhase = 0 Then
'Inform the user of whatever you want, perhaps ask the first question
CurrentPhase = 1 'when the button is pressed we will process the first question
ElseIf CurrentPhase = 1 Then
If First Answer is correct then
Ask next question
CurrentPhase = CurrentPhase + 1
Else
GuessCount = GuessCount + 1
If GuessCount exceeds limit Then
CurrentPhase = 99 'failed
End If
End If 'Answer not correct

ElseIf CurrentPhase = 2 then
If Second Answer is correct Then
... and so forth

ElseIf CurrentPhase = LastQuestion Then
If Last Answer is correct Then
Congratulations, you've passed the test.
Else
...
End if
End If


If CurrentPhase = 99 Then
' Print you've failed the test
End if

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum