Hi there,

I'm trying to create a math quiz that reads a series of questions from a sequential access flat file. The file should contain a series of numbers, separated by commas. The first field(value) should be the first number for the problem, the second should be the second number for the problem, and the third number should identify the problem type like the following:

1,2,0
2,3,1
4,5,2...

I'm modifiying a previous version that randomly generates 10 questions, stores the answer in an arry and checks the array to see if the question has been already asked.

I need to load the questions from the file into array structures when loading the form. The application should then read the data from the arrays as a user progresses through the quiz.

Any suggestions on how to get this started?

Thanks.

I don't have access to my dev environment for the next few days so I'll try from memory. You can read all lines of the file into an array as follows

Dim questions() As String = System.IO.File.ReadAllLines(filename)

Then you can step through each question as

For Each line As String In questions

    Dim fields() As String = line.Split(",")
    Dim num1 As Integer = CInt(fields(0))
    Dim num2 As Integer = CInt(fields(1))
    Dim num3 As Integer = CInt(fields(2))       
    .
    .
    .

Next

You may have to verify the System.IO.File.ReadAllLines part.

Thanks for the information.

So to clarify, I would go from num1 through num30 (3 numbers in a row x 10 rows) since it's 10 questions?

Once I have access to those fields, how can I display them efficiently to the user one question at a time?

You could declare two class level variables; one to contain the strings (the question array) and another to indicate which is the current question. You could code a Sub which splits the current question into the separate fields, then copies the values into text fields for display. Then, after the user enters his or her answer and clicks on a SUBMIT button, that button code would evaluate the answer, update the score (if any), increment the question number then call the Sub to display the next question. If there are no more questions then the final results could then be displayed.

Thanks! That makes a lot more sense now.

Okay, Here's what I came up with.

This block is in an independent sub procedure I created.

        Dim inputFile As IO.StreamReader
        If IO.File.Exists("numbers.txt") Then
            inputFile = IO.File.OpenText("numbers.txt")

            Dim strLineOfText As String
            Do Until inputFile.Peek = -1
                strLineOfText = inputFile.ReadLine
                myArray = strLineOfText.Split(CChar(","))
                value1(currRow) = CInt(myArray(0))
                value2(currRow) = CInt(myArray(1))
                probTypes(currRow) = CInt(myArray(2))
                currRow += 1
            Loop
            inputFile.Close()
        Else
            MessageBox.Show("Can't Find the File", "Math Quiz", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If
        currRow = 0

I had to "reset" currRow to zero since I was getting a IndexOutOfRangeException Error. When I was debugging.

I created another sub and put the display logic with a separate class-level question counter variable to keep track of the number of questions I've asked.

My click event to calculate had the logic to keep track of questions answered correctly and incorrectly. When the user reached 10 then it would bring up a message box advising how many they where answered correctly out of 10.

Granted, This may seem elementry to some programmers but I'm proud I figured it out.

I was taught that when doing database "stuff" the generally accepted practice is to open the connection as late as possible, do what you gotta do, then close the connection as soon as possible. Also, as someone who learned to code long before all of the fancy interfaces and abstractions, I prefer to have as few layers between me and my data as possible. As such, my preference is to have files open for only a short time and to read all of the data at once (unless processing extremely large files).

Based on that, my preference would be to avoid StreamReader (and the associated open, end-of-file test, read, close) and just read the entire file into the "question" array in one step as I showed you earlier. That reduces your code to a simple

if the file exists
    read it
else
    display "file not found message"
end if

and you can split the lines into fields at that point (that way you can verify up front that all lines are valid) or step through them one at a time as needed. There are many ways to attack this and no particular way is the "right" way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.