School Project - Trivia Game

Zip

Member
Joined
Mar 25, 2011
Messages
14
Programming Experience
Beginner
I need help writting a Trivia Game for a school project. The teacher wanted us to get used to looking to forums and google for help so he refuses to help us for this particular summative.

What I want to know is...

- how to use a streamreader to read questions randomly from a text document located in the debug folder (I have 10 questions all in 1 text file in the debug folder, and the same for all of my answers)
- how to use a streamreader and split function to read answers from a text document located in the debug folder to a combobox.

My program is set up like this.

I have a forum "Game" with 3 buttons, one for each catagory. When the catagory is selected a second form "Question" loads with a combobox and a lable. In the lable I want to display a question from my text file randomly, then have the corresponding set of answers displayed in the combobox. When the player selectes the right answers they get points.

My current code looks like this, and dosen't work :(

Private Sub btnDogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDogs.Click
Dim strDogA As String
Dim strDog() As String
Dim objDogs As New System.IO.StreamReader("Dog_Question.txt")
frmQuestions.lblQuestion.Text = objDogs.ReadLine
strDogA = "Dog_Answer.txt"
strDog = strDogA.Split("/")

frmQuestions.cmbAnswers.Items.Add(strDog)

frmQuestions.Visible = True
Me.Visible = False

I know that .ReadLine is only to read the first line of text in a text document and thats why it's not reading the questions properly, I only put it as that because I don't know how to code it so that it will read a random 1 of 10 questions from a text document, and needed to have it read something just to test it.

Just so that you know, this is how my questions and answers are set up.

Questions - All of my questions for each catagory are in 1 text file like this
Question 1
Question 2
Question 3

Answers - I have it set up like multiple choice so my answers are all in 1 text file like my questions but each option is separated by / like this.
right answer/wrong answer/wrong answer
wrong answer/ right asnwer/ wrong answer

I appologize if what I am asking is confusing, I know that the way I have things organized and worded can be a bit confusing, I was just trying to include everything so that you would know exactly what I am trying to do with the code.
 
Whenever you want to do something random, it pretty much means that you're going to be using the Random class. To use the Random class, you create a single instance and then call one of its methods each time you want a random number. Generally, that method will be Next, which returns an Integer. You can optionally specify upper and lower limits on the number too. Once you have the number, you use it in whatever way is appropriate for your app, e.g. an interval for a Timer or an index into an array or collection. You should start by reading the documentation for the random class and then look for examples online if you feel you need them. Don't be afraid to experiment with the class in a test project, isolated from all the other functionality of your assignment.

In your case, if the assignment permits, I would tend not to use a StreamReader directly. It would be simpler to call File.ReadAllLines, which will return a String array containing the lines from a file. If you must use a StreamReader then call ReadToEnd to get the entire contents of the file as a single String. You can then call Split on that String to break it into an array containing the lines. Call the Split overload that takes a String array as an argument and use a String array with Environment.NewLine as the one and only element. Again, consult the documentation first.
 
Thanks,
I have used Randomization before in other programs, this will be my third year taking this class, I just don't understand how to use it in this situation.

I would appreciate it if you could show me using sample code for how it would work with my program, it makes it easier for me to understand.
 
I already explained how it will work in your app.
Once you have the number, you use it in whatever way is appropriate for your app, e.g. ... an index into an array or collection.
It would be simpler to call File.ReadAllLines, which will return a String array containing the lines from a file.
You can then call Split on that String to break it into an array containing the lines.
 
I would always suggest starting with the documentation: Random Class (System). You'll find use examples right there.

Since we know you're doing this for school we're going to assume a certain amount of this project (100%) is aimed at you learning new material. With that in mind we'll try and provide you with areas you can go and learn the subject matter.

If you've made an attempt and are stuck you can come back with the code you have so far and we can try and show you why it's not working.
 
My only issue is that the project is due on Monday. And I have been stuck for almost 2 weeks now. I am familiar with using the Random class as I have almost 3 years of experience using VB I just don't know where to start here. I will give it a try again and post more code later tonight
 
I've told you where to start. Load the file into an array, generate a random number and then use that number as an index into the array. Tada! You've got an element from the array chosen at random.
 
Thanks,
Do you have any links to the documentation of the Array, I haven't used them in a while, I will try it and post the code :)
 
Are you saying to do something like this?
I keep getting an error saying that "CatQ is not declared" but in the example our teacher showed us he never showed how to declare it. I don't think he even had his declared but his program still ran fine... How would I go about declaring it?
VB.NET:
[I]Public Class frmGame
    Dim intQuestionNum As Integer
    Dim rndRand As New Random
    Dim lstCatsQuestions As New List(Of CatQ)
    Dim intQuestionNumber As Integer[/I]
[I]Private Sub btnCats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCats.Click[/I]
[I]        Dim rndRand As New Random[/I]
[I]        intQuestionNumber = rndRand.Next(0, lstCatsQuestions.Count)
        frmQuestions.lblQuestion.Text = lstCatsQuestions(intQuestionNumber).strQ[/I]
[I]        Dim intRightAnswer As Integer
        intRightAnswer = rndRand.Next(1, 5)[/I]
[I]        If intRightAnswer = 1 Then
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strAnswer)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA1)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA2)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA3)
        ElseIf intRightAnswer = 2 Then
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA1)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strAnswer)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA2)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA3)
        ElseIf intRightAnswer = 3 Then
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA1)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA2)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strAnswer)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA3)
        ElseIf intRightAnswer = 4 Then
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA1)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA2)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strFalseA3)
            frmQuestions.cbxAnswers.Items.Add(lstCatsQuestions(intQuestionNumber).strAnswer)
        End If[/I]

[I]        frmQuestions.Visible = True
        Me.Visible = False[/I]
[I]    End Sub[/I]
[I]    Private Sub frmQuestions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sRead As IO.StreamReader = New IO.StreamReader("Cat_Questions.txt")
        Dim strTemp As String
        Dim strTempArray() As String[/I]
[I]        strTemp = sRead.ReadLine
        strTempArray = strTemp.Split("/")
        lstCatsQuestions.Add(New CatQ(strTempArray(0), strTempArray(1), _
                                                       strTempArray(2), strTempArray(3), strTempArray(4)))[/I]
[I]        Do While (sRead.Peek <> -1)
            strTemp = sRead.ReadLine
            strTempArray = strTemp.Split("/")
            lstCatsQuestions.Add(New CatQ(strTempArray(0), strTempArray(1), _
                                               strTempArray(2), strTempArray(3), strTempArray(4)))
        Loop[/I]

[I]    End Sub[/I]
[I]    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If frmQuestions.cbxAnswers.Text = lstCatsQuestions(intQuestionNumber).strAnswer Then
            MsgBox("Right!")
        Else
            MsgBox("Wrong!")
        End If[/I]
[I]        frmQuestions.cbxAnswers.Items.Clear()
        frmQuestions.cbxAnswers.Text = ""[/I]

[I]        frmQuestions.lblQuestion.ResetText()[/I]
[I]    End Sub
[/I]
 
Last edited by a moderator:
'CatQ' is a class that contains data and/or behaviour relating to a quiz question on cats. Your 'lstCatsQuestions' is a list containing multiple instances of that class, i.e. multiple questions on cats.
 
So how would I create the cat class? I know how to make classes, but what behaviours would I need to give it to make it work?
 
Well, I guess I would need it to contain all 10 of my cat questions, and then I would need it to randomly call a question at a time and display it in frmQuestions.lblQuestion.text

But how would I load my questions into the class? would it be best for this particular program to load them in one by one using if statements, or load the text file containing all the questions? If I load them through the text file how would I get it to differentiate each question? and call them on separatley?
 
Back
Top