String Arrays

Feb 11, 2016 at 2:14pm
Helleue :)
I am trying to create a quiz where the user can set their own questions, and answers, and what i want to do is having the users questions and answers inside of an array. I have tried using "vector", and "new" but haven't figured out how to use them with string, since the program crashes right after it waits for the user input, my guess is it has something to do with the arrays(que & ans).
So my request is if someone can hint a way to fix it, or a link so i can read up on it :)

The current code: (it got flaws, and as i said the vector thing doesnt work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void addQaA() {
	int amtQ;
	string question, answer;
	cout << "Amount of questions: ";
	cin >> amtQ;

	for (int i = 1; i <= amtQ; i++) {
		cout << "Enter Question: ";
		vector<string> ans;
		vector<string> que;

		getline (cin, question);
		que.push_back(question);
		que[i] = question;

		getline(cin, answer);
		ans.push_back(question);
		ans[i] = answer;
	};
}
Last edited on Feb 11, 2016 at 2:15pm
Feb 11, 2016 at 3:33pm
Lines 14 and 18 are unnecessary (and wrong).

Line 13: The push_back adds the question to the que vector. Since you start i at 1, que[1] is undefined. Only que[0] exists after you push the first question on the vector. Ditto for answer.

Line 17: I think you want to be pushing answer, not question.

Lines 9-10: These vectors will go out of scope when you exit the for loop.

Last edited on Feb 11, 2016 at 3:35pm
Feb 11, 2016 at 3:43pm
Wow, didn't think i would miss the line 13 thingy, anyways thank you so much!!! :)
Topic archived. No new replies allowed.