istream_iterator insert one multiple choice questions

I'm creating a Trivia game and I have Question object to hold multiple choice questions and store it into a VecQuestions. My problem is when the text file have more than one multiple choice questions it only inserts only first multiple choice questions into the vector?

test Data from the text file:
1. What programming language is used in this Course? 3

1. C
2. Pascal
3. C++
4. Assembly

2. What compiler can you use to compile the programs in this Course? 4

1. Dev-C++
2. Borland C++Builder
3. Microsoft Visual C++
4. All of the above
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <fstream>
using namespace std;
class Question
{
public:
	std::string question;
	int correctIndex;
	std::map<int, std::string> answers;


	friend std::istream &operator >> (std::istream &is, Question &q) {

		getline(is, q.question, '?');
		is >> q.correctIndex;
		std::string line;
		while (getline(is, line) && line.empty())
			;
		while (is && !line.empty())
		{
			int id;
			std::string ans;
			char pt;
			std::stringstream sst(line);
			sst >> id >> pt;
			if (!sst || id == 0 || pt != '.')
				std::cout << "parsing error on: " << line << std::endl;
			else {
				getline(sst, ans);
				q.answers[id] = ans;
			}
			getline(is, line);
		}
		return is;
	}

};
    

int main()
{
	std::ifstream readFile("questions.txt");
	std::vector<Question> questions((std::istream_iterator<Question>(readFile)), std::istream_iterator<Question>());
	std::cout << questions.size() << std::endl;
	for(auto i: questions)
	{
		cout << i.question << endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.