' Wyo Basic Ch. 9 Demo #5
' Mr. Minich
' Purpose - to demonstrate the use of sequential access files
Option Explicit
Dim mstrQuestion(1 To 10) As String ' array of questions to ask user; module-level
Dim mstrAnswer(1 To 10) As String ' array of answers to questions; module-level;
' parallel array to mstrQuestions
Private Sub cmdSubmit_Click()
' Purpose: Obtains user answer to a question and checks the answer. Updates
' user score. When all questions have been asked, the user is
' prompted and his/her score is recorded to a sequential access
' text file for permanent safe-keeping.
' Precondition: User will have typed his answer into txtUserAnswer
' Postcondition: User's score (Score) will be modified, the text box, txtUserAnswer,
' will be cleared, and a new question will be presented unless all
' questions have been asked. User's score will be stored in a
' sequential access file.
' Called by: None
' Calls: None
Static intScore As Integer ' user's score; static scope
Static intQuestionNumber As Integer ' question number that is being asked; static scope
Dim strUserName As String ' user's name
intQuestionNumber = intQuestionNumber + 1 ' moving to the next question
If UCase(txtUserAnswer.Text) = mstrAnswer(intQuestionNumber) Then
intScore = intScore + 1
Else ' adding to or subtracting from user's score
intScore = intScore - 1
End If
lblScore.Caption = intScore ' updating score in label
txtUserAnswer.Text = "" ' clearing text box for next answer
txtUserAnswer.SetFocus
If (mintQuestionNumber < 10) Then ' game is over when all questions have been used
lblQuestion.Caption = mstrQuestion(mintQuestionNumber + 1)
Else
Open "Z:\Temp\Ch9Demo1HighScore.txt" For Output As #2
UserName = InputBox("What is your name?")
Print #2, strUserName
Print #2, intScore ' writing high score to a sequential access
Close #2 ' text file
MsgBox UserName & ", your score of " & intScore & " has been recorded. Goodbye."
End If
End Sub
Private Sub Form_Load()
'Purpose: To set up the quiz when the program executes. Reads questions and
' answers into respective arrays and presents the first question.
'Precondition: None
'Postcondition: Questions and answers have been stored in the mQuestion and mAnswer
' arrays, respectively. The first question will be presented.
'Called by: None
'Calls: None
Dim intLoop As Integer ' loop variable
Open "Z:\Temp\Ch9Demo1QuestAnswer.txt" For Input As #1
For intLoop = 1 To 10 ' reading all questions from file into array
Input #1, mQuestion(intLoop)
Input #1, mAnswer(intLoop)
Next intLoop
Close #1
lblQuestion.Caption = mstrQuestion(1) ' presenting the first question
End Sub