Results 1 to 7 of 7

Thread: Testing Software (Quiz) - Generate random question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Testing Software (Quiz) - Generate random question

    Hello,
    I would like ask you for help with me school project.
    I need create quiz: I have menu form with some welcome screen and start button, which show next form. At this form are labels for questions and answers, 3 radio button, and one button. This one button is really PROBLEM. I need help with code.
    At this button must be some condition for right answer (using a,b or c from Txtfile) and if is checked right radio button (with right answer) then x=+1, if checked wrong radio button, then x= +0.(I think this code i can create, but i need help with generating new questions and end quiz after 10question.)
    Next function of this button must be generate next random question from Txtfile. And After 10 Question show new Form where will be value X, which is number of right answers.
    I have some code, here is:

    My txt file:
    Code:
    //Question|Answer1|Answer2|Answer3|right answer(a,b,c)
    
    Question 1?|Answer1|Answer2|RightAnswer3|c
    Question 2?|continue|vfvfvfvfv|gtgtgtgg|a
    Question 3?|njmbnmb|right answer|right!|b
    Question 4?|wasdw|cdcdc|right!|c
    Question 5?|good!|fefefef|frfrfrfrf|a
    Code:
    // This is code to load txt file and split line with "|". This is Form code
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim soubor As New IO.StreamReader("textfile1.txt")
            While Not soubor.EndOfStream
                Dim txtline() As String = soubor.ReadLine().Split("|")
                Dim question As String = txtline(0)
                Dim ans1 As String = txtline(1)
                Dim ans2 As String = txtline(2)
                Dim ans3 As String = txtline(3)
                Dim rightanswer As String = txtline(4)
            End While
        End Sub
    
    // Button code. What u think, can this code work? But there is lots of wrongs. Its just for generate new question but there isnt condition for right answer and code to ned quiz after 10 quesitons.  
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            read the whole textfile; textline=soubor.readtoend() // i dont know how edit this code, my txt file is named textfile1
    
            Dim lines() As String = txtfile.split(vbCr)
            Dim c As Integer = textline.count // My VB 2010 said: its not declared "textline.count" and "txtfile.split(vbCr)"
    
            Randomize()
            Dim line As Integer = CInt(Int((c * Rnd()) + 1))
    
            Dim ques As String = lines(line)
            Dim Q() As String = ques.Split("|")
    
            Dim question As String = Q(0)
            Dim ans1 As String = Q(1)
            Dim ans2 As String = Q(2)
            Dim ans3 As String = Q(3)
    
            Label1.Text = question
            Label2.Text = ans1
            Label3.Text = ans2
            Label4.Text = ans3
    
            
        End Sub
    End Class

  2. #2
    Member
    Join Date
    Aug 2006
    Posts
    41

    Re: Testing Software (Quiz) - Generate random question

    Code:
     Dim q1(10), a1(10), a2(10), a3(10), ca(10) As String ' load questions , answers & correct answer
        Dim value As Integer
        Dim x As Integer 'result
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim selectedanswer As String = ""
    
            If RadioButton1.Checked = True Then selectedanswer = "a"
            If RadioButton2.Checked = True Then selectedanswer = "b"
            If RadioButton3.Checked = True Then selectedanswer = "c"
    
            If selectedanswer = ca(value) Then x = x + 1
            loadquestions()
    
    
        End Sub
    
        Public Sub loadquestions()
    
    
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 10. 
            value = CInt(Int((10 * Rnd()) + 1))
            Label1.Text = q1(value).ToString
            Label2.Text = a1(value).ToString
            Label3.Text = a2(value).ToString
            Label4.Text = a3(value).ToString
    
        End Sub
    i didn't test it, but i think it will be closer to correct one

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Testing Software (Quiz) - Generate random question


    And which part from my code i should use? I used just streamreader part which split line at txt file and load it, but when i use it, then after click to button which load first question then error.
    http://oi60.tinypic.com/20pbtjo.jpg (Screen with code and error.)
    I have txt with 50questions and i need choise random 10questions every time when i start quiz.
    Code:
    Public Class Form2
    
        Dim q1(10), a1(10), a2(10), a3(10), ca(10) As String ' load questions , answers & correct answer
        Dim value As Integer
        Dim x As Integer 'result
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim soubor As New IO.StreamReader("textfile1.txt")
            While Not soubor.EndOfStream
                Dim txtline() As String = soubor.ReadLine().Split("|")
                Dim question As String = txtline(0)
                Dim ans1 As String = txtline(1)
                Dim ans2 As String = txtline(2)
                Dim ans3 As String = txtline(3)
                Dim rightanswer As String = txtline(4)
            End While
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim selectedanswer As String = ""
    
            If RadioButton1.Checked = True Then selectedanswer = "a"
            If RadioButton2.Checked = True Then selectedanswer = "b"
            If RadioButton3.Checked = True Then selectedanswer = "c"
    
            If selectedanswer = ca(value) Then x = x + 1
            loadquestions()
        End Sub
    
        Public Sub loadquestions()
    
    
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 10. 
            value = CInt(Int((10 * Rnd()) + 1))
            Label1.Text = q1(value).ToString
            Label2.Text = a1(value).ToString
            Label3.Text = a2(value).ToString
            Label4.Text = a3(value).ToString
    
        End Sub
    
    End Class
    Last edited by alien8k; Apr 13th, 2015 at 10:05 AM.

  4. #4
    Member
    Join Date
    Aug 2006
    Posts
    41

    Re: Testing Software (Quiz) - Generate random question

    hi
    sorry for late
    here is your code
    also the source code attached
    choose the test text file with starting the software
    enjoy
    WindowsApplication3.zip

    Code:
    Public Class Form1
        Dim q1(10), a1(10), a2(10), a3(10), ca(10) As String ' load questions , answers & correct answer
        Dim value As Integer = 0
        Dim counter As Integer = 0
        Dim x As Integer 'result
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label3.Text = Label3.Text + 1
    
            Dim selectedanswer As String = ""
    
            If RadioButton1.Checked = True Then selectedanswer = "a"
            If RadioButton2.Checked = True Then selectedanswer = "b"
            If RadioButton3.Checked = True Then selectedanswer = "c"
    
            If selectedanswer = ca(value) Then x = x + 1
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            If Label3.Text = 10 Then
                MsgBox("Your Result is " & x)
                End
    
            End If
    
    
            loadquestions()
    
        End Sub
        Public Sub loadquestions()
    
    
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 10. 
            value = CInt(Int(((counter - 1) * Rnd()) + 1))
            Label1.Text = q1(value).ToString
            RadioButton1.Text = a1(value).ToString
            RadioButton2.Text = a2(value).ToString
            RadioButton3.Text = a3(value).ToString
    
        End Sub
    
        Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            OpenFileDialog1.ShowDialog()
    
    
    
    
            Dim soubor As New IO.StreamReader(OpenFileDialog1.FileName)
            While Not soubor.EndOfStream
                Dim txtline() As String = soubor.ReadLine().Split("|")
                q1(counter) = txtline(0)
                a1(counter) = txtline(1)
                a2(counter) = txtline(2)
                a3(counter) = txtline(3)
                ca(counter) = txtline(4)
                counter = counter + 1
            End While
            loadquestions()
        End Sub
    End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Testing Software (Quiz) - Generate random question

    GREAT, THANKS.
    I edited "Dim q1(50), a1(50), a2(50), a3(50), ca(50) As String" for txt with 50questions, i think its works.
    And i have just last one problem. Questions are repeated.
    I edited end of program, after msgbox with result, the program will jump to menu, where is start button. after click on start button program will start from where is quiz. And i need each question one once at every quiz. If i will complete quiz and start it again, then again every question only once. How i can fix it??
    Last edited by alien8k; Apr 14th, 2015 at 11:43 PM.

  6. #6

  7. #7
    Member
    Join Date
    Aug 2006
    Posts
    41

    Re: Testing Software (Quiz) - Generate random question

    Hi
    as i understand from you
    you want to load 50 question and choose only 10 questions ( not repeated ones) in exam
    ok, this the attached code will do that
    but it starting up the software load a question file with 50 QUESTIONS.
    enjoy

    WindowsApplication3_2.zip

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width