Were trying to use external file (txt or CSV) in order to create a file stream in C#. The data in the file is that of a quiz game made of :
1 short question 4 possibles answers 1 correct answer
The program should be able to tell the user whether he answered correctly or not.
I’m looking for an example code/algorithm/tutorial on how to use the data in the external file to create a simple quiz in C#. Also, any suggestions on how to construct the txt file (how do I remark an answer as the correct one?). Any suggestions or links? Thanks,
There’s really no set way to do this, though I would agree that for a simple database of quiz questions, text files would probably be your best option (as opposed to XML or a proper database, though the former wouldn’t be completely overkill).
Here’s a little example of a text-based format for a set of quiz questions, and a method to read the questions into code. Edit: I’ve tried to make it as easy as possible to follow now (using simple constructions), with plenty of comments!
File Format
Example file contents.
Code
This is just a simple structure for storing each question in code:
You can then read the questions from the file using the following code. There’s nothing special going on here other than the object initializer (basically this just allows you to set variables/properties of an object at the same time as you create it).
Of course, this code is rather quick and basic (certainly needs some better error handling), but it should at least illustrate a simple method you might want to use. I do think text files would be a nice way to implement a simple system such as this because of their human readability (XML would be a bit too verbose in this situation, IMO), and additionally they’re about as easy to parse as XML files. Hope this gets you started anyway…