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?.
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.
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
@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.