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.
 
You seem to have missed the point. In post #12, I specific said:
'CatQ' is a class that contains data and/or behaviour relating to a quiz question on cats.
It represents ONE question. I then said:
Your 'lstCatsQuestions' is a list containing multiple instances of that class, i.e. multiple questions on cats.
You have a list containing ALL the questions. I've been saying right from the beginning that you need to generate a random number and then use that number as an index. You use it to index that list. If you have a list containing 10 CatQ objects that each represent one question, you generate a random number from 0 to 9 and then get the CatQ object at that index in the list. Hay presto! You've got one question randomly selected from the 10.

I think that you would benefit by reading these:

Unique, Random Selections from a List
(.NET 3.5) Randomise a List of Items
 
Okay, well what about the corresponding set of answers? How would I make sure that when I generate the questions the right set of answer options show up in my Combobox?
 
You don't get what I am asking, I understand how to randomize. What I don't get is how I am supposed to list the questions and answers.

are you saying I need to creat a CatQ for all questions? like this

Public Class CatQ
frmQuestions.lblQuestion.text = "Question 1"
end class

Public Class CatQ2
frmQuestion.lblQuestion.text = "Question 2"

Or do I make a second list inside the class containing each question like this?

Public Class CatQ
Dim lstCatQuestion as new list (Of String)
end class

I have NO clue how to creat the list. Why can't you just give me a sample code?
 
Questions placed in an array. Array index selected randomly.

Here is some code to help you get started:

VB.NET:
		Dim nextques As Integer, answer As String
		Dim questions(6) As String
		Dim randques As New Random
		nextques = randques.Next(1, 6)
		questions(1) = "How many legs does a cat have?"
		questions(2) = "What color is a black cat?"
		questions(3) = "How many lives does a cat have?"
		questions(4) = "What is your cat's name?"
		questions(5) = "What does a cat eat?"
		answer = InputBox(questions(nextques))
 
Thanks,
I am asuming this is the code that will go in my class. It's just getting so frustrating becuase It's already over due and I haven't really made much progress. >.< Thanks for the code, I will try something like that in my program and see if that helps get me started.
 
Actually, that's just a simple little program that will go in a button's Click event. Just cut and paste the code into a new form with a button control to see how it works.

The questions array should have a matching answer array with the correct answers. Compare the index of one array with the matching index in the other for the correct answer.
 
This is just a simple program with no classes.

Copy and paste this code into a new form with a button control to see how it works. You need one array for the questions and a matching array for the answers. The program uses the same index to compare the user's answer to the question. Make up more arrays for the other trivia categories.

If you need to use classes for your project, then just take it from there.


VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim nextques As Integer, answer As String
		Dim questions(6) As String
		Dim answers(6) As String
		Dim randques As New Random
		nextques = randques.Next(1, 6)
		questions(1) = "How many legs does a cat have?"
		questions(2) = "What color is a black cat?"
		questions(3) = "How many lives does a cat have?"
		questions(4) = "Does a cat chase mice?"
		questions(5) = "What does a cat like to drink?"
		answers(1) = "4"
		answers(2) = "black"
		answers(3) = "9"
		answers(4) = "yes"
		answers(5) = "milk"
		answer = InputBox(questions(nextques))
		If answer = answers(nextques) Then
			MessageBox.Show("Correct")
		Else
			MessageBox.Show("Wrong")
		End If
	End Sub
 
Thanks,
I think the teacher wanted us to have the questions and answers read from a text document located in the debug folder using a Streamreader, but If I can't figure that out in time I will use the code you just provided.
 
I still don't get it. Can you comment the code so I can know what you're doing in each part?
Where is the Array in your code?

my questions were going to be multiple choice, and I was originaly going to have 4 potential answers displayed in a combobox then when the right one was selected points would be awarded. but I can do it so they type the asnwer in a textbox instead.
 
It looks like you don't even know what an array is. Are you repeating this course for the third time? Copying code is not going to teach you anything. It's cheating and it's not fair to the other students who are doing their own work.

An array is an indexed collection of variables with the same type. You Dim the array using an index, for example:
Dim myarray(6) As String.
Then you reference each array element by its index, for example:
myarray(1) = "Hello"

I suggest you write your own code and try to do what you intended to do in the first place, even if you have to stay up all night to do it. Maybe you might even learn something from your mistakes. Also look up the reference in a previous post on arrays and see if you can learn something from that. You should also be able to find some information in your textbook. Have you tried that?
 
Back
Top