0

The function getQuiz() accepts 2 parameters the line number and the path of a csv file. It splits out thhe question and options then checks against the correct answer which works fine. I want to be able to return the score at the end of the quiz. The problem lies with fact I'm looping through the function. The score resets each time.

How can I retrieve the score at the end of the quiz? This is how the function is being called:

'Calls the getQuiz function
Sub Main()
    Dim i As Integer

    For i = 2 To 6
        getQuiz(i, "C:\Users\HisEasy.csv")   
    Next

End Sub


Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As String
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long
    score = 0
    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            score = n + 1
            Return score
        End If

    End Using

End Function
2
  • You could add up the score in the Main part. Simply return 0 or 1 as appropriate from the getQuiz function. Also, you may be interested in the File.ReadAllLines Method which reads all the lines from a file into an array in one go, so you wouldn't have to keep reading the file for every question. Commented Nov 20, 2017 at 9:07
  • Isn't the obvious thing to simply move the declaration of the score variable outside the getQuiz method? Commented Nov 20, 2017 at 9:11

2 Answers 2

1

You could use the return value of your function getQuiz(), which you are doing nothing with at the moment

'Calls the getQuiz function
Sub Main()
    Dim i As Integer
    Dim score as Integer

    For i = 2 To 6
        If getQuiz(i, "C:\Users\HisEasy.csv") = True then
            score += 1
        End If
    Next

End Sub


Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As Boolean
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long

    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            Return True
        End If

    End Using

End Function

Also you should really consider Andrew Morton's comment regarding the workflow of your application, since loading the entire quiz content every time getQuiz is called is pretty redundant here.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just

'Calls the getQuiz function
Sub Main()
    Dim i As Integer
    Dim score as long

    score = 0

    For i = 2 To 6
        'Add score after each call
        score = score + getQuiz(i, "C:\Users\HisEasy.csv")   

    Next

End Sub

'Use Long instead of String as return type
Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As long
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long
    score = 0
    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            score = n + 1
            Return score
        End If

    End Using

End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.