using getline to parse from a text file.

Hi!

So, I have a quick question. I'm trying to make a program where it reads a text file which contains "blocks" of questions like:

Type: multiple_choice
Question
Num_options: number of options
a) option1
b) option2
c) option3
d) option4
Letter of answer


and

Type: short_answer
Question
Answer text


what I'm trying to do is use parse those blocks and have it get handled by the classes, MultipleChoice and ShortAnswer.
I'm planning on using getline as a virtual function and have an if statement like
"if (string == "multiple choice)
** then get it handled by the MultipleChoice class "

and same goes for the short answer blocks. My question is how do you do this?.
Last edited on
NewatCplusplus wrote:
Explain like I'm 5.
http://xkcd.com/1364/
Sorry! C++ is not my forte.
Is that how the input file has to look? Or can you choose to make it easier to read in? Because as it is now, your input file has lots of text that needs to be ignored or validated by your program.
yeah, that's how it's suppose to look.
Your answer is not clear, please be more specific. There is a reason I worded the question the way I did.
Last edited on
This is an example of the input file called "Quiz.txt"

Type: multiple_choice
Which of the following are key concepts in Object-Oriented Programming?
Num_options: 5
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Abstraction
e) All of the above
e
Type: short_answer
Encapsulation allows you to separate the interface from the __________
Implementation
Type: short_answer
Other than a copy constructor and an overloaded assignment operator, what is the missing big 3 function?
Destructor
Type: multiple_choice
What does the keyword "static" do to a member variable?
Num_options: 4
a) It means the member variable cannot change
b) It means the member variable is shared among all objects of that class
c) It means the member variable is inherited by derived classes
d) It means the member variable cannot be downcasted
b
Type: multiple_choice
Suppose B is a subclass of A. Which of the following is correct?
Num_options: 4
a) B is a parent class of A
b) B cannot be instantiated
c) A can be upcast to B
d) B can access any protected member variables of A
d
Please read carefully:
LB wrote:
Is that how the input file has to look? Or can you choose to make it easier to read in?
I don't understand your question. Do you mean the input file as in the text file? or as in how I worded my question?
The text file is the input file. My question is, do you decide the format, or does someone else? I don't know how to be any more clear.
Last edited on
@LB
I think it'd be fair to gather by now that the input format is not modifiable.

@NewatCplusplus
It's pretty simple as-is. Every question starts with the word "Type:", and files a fixed format after that.

Make a function that reads the first line and figures out the type of question, then calls another function that reads a specific type. For your example, you only need two additional functions: one to read multiple-choice and one for a short answer.

It would be nice if you made the additional functions input operators of the appropriate class. That way, your main reading function only needs to instantiate an appropriate object and then let that object read the remainder of the question.

Hope this helps.
Topic archived. No new replies allowed.