Help finding issue with multidimensional vector

So, the program compiles but there is a logical error somewhere that is crashing the program. Can someone please help me find it.

Some lines are seemingly commenting the obvious, because it is for college.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std; //STD library

int main()
{
	vector<string> studName;         //
	vector<char> corrAns;            //
	vector<char> studAns;            //
	vector<int> numCorr;             //
	char correctAns;                 //Fairly
	char studentAns;                 //Self
	string nameStud;                 //Explanitory
	int numStud = 0;                 //
	int questions = 0;               //

	cout << "How many questions will be asked?" << endl; //How many questions to score
	cin >> questions;   //cin num of questions


	for(int x=0; x<questions; x++) {  //FOR loop to populate correct answers in vector
		cout << "Enter the answer for question " << x+1 << "[A,B,C,D]: " << endl; //cout question
		cin >> correctAns;  //cin answer
		corrAns.push_back(correctAns);  //pushing each answer into vector
	}

	cout << "Enter the number of students to test: " << endl; //asking for number of students to test
	cin >> numStud;  //cin num of students

	


	vector< vector<char> > studentAnswers(numStud, vector<char>(questions)); //Multi-dimensional vector to keep track of students, answers, percentage.

	for(int i=0; i<numStud; i++) {    //FOR loop to populate names of students being tested
		cout << "Enter the name of student " << i + 1 << ": " << endl;  //cout question
		cin >> nameStud;   //cin name of students
		studName.push_back(nameStud);  //push back each name into vector
		vector<char> row;  //
		
		for(int j=0; j<questions; j++) {   //FOR loop to populate each students answers
			cout << "Enter the answer for question " << j+1 << ": " << endl;  //cout question
			cin >> studentAns;   //cin answer
			row.push_back(studentAns);  //push back name into vector
		}

		studentAnswers[i] = row;  //push back answer into vector
		row.clear();  //call in destructors
	}

	for(int k=0; k<numStud; k++) {  //FOR loop for students correct answers
	
		cout << studName[k] << " was correct on the following questions: "; //cout correct?
		
		for(int l=0; l<questions; l++){  //FOR loop to compare and output ans
			if (studentAnswers[k][l] == corrAns[l]) { //compare
				cout << l << " ";  //cout questions correct
				numCorr[k] += 1;  //counter
			}
		}

		cout << endl;  //spacer
		cout << "for a total of " << numCorr[k] << " answers." << endl; //total correct answers
		cout << "And a percentage of " << ((numCorr[k]/questions) * 100) << "%." << endl << endl;  //percentage correct
	}

	system("pause"); //system pause
	return 0; //return
}



Thanks for any thoughts or opinions!!!
Last edited on
It would help if we knew what it was trying to do as well as where you think the problem is.
Well, the problem is after the user inputs student answers and then its supposed to compare studAns to corrAns and output as seen in the code. It crashes and I believe it has something to do with the way I'm populating the multidimensional vector.
Bump
Bump
Here is some debugging output. Everything I typed is in bold.

(gdb) run
Starting program: ./a.exe
[New thread 1252.0x11b4]
How many questions will be asked?
2
Enter the answer for question 1[A,B,C,D]:
A
Enter the answer for question 2[A,B,C,D]:
B
Enter the number of students to test:
2
Enter the name of student 1:
Eric
Enter the answer for question 1:
Eggs
Enter the answer for question 2:
Enter the name of student 2:
Enter the answer for question 1:
Bill
Enter the answer for question 2:
Eric was correct on the following questions:

Program received signal SIGSEGV, Segmentation fault.
0x004018c4 in main () at 39.cpp:65
65 cout << "for a total of " << numCorr[k] << " answers." << endl; //total correct answers
(gdb) print k
$1 = 0


There is an attempt to access element 0 of numCorr. Since you create numCorr and then never make it big enough to have any data put into it, it's not surprising that trying to fetch data from it causes a segFault.

I note also that on some requests for data, I was unable to enter anything as the programme skipped straight on to the next section without waiting for me to enter anything. I suspect because I entered a word of four letters (Eggs) rather than a single letter. When you get it all working, it's worth defending against that.

I can also provoke a segFault by entering different data, with the segFault on this line:

60 numCorr[k] += 1; //counter

It's the same problem as before. Trying to put something into numCorr[0] which does not exist. You've hit this same problem repeatedly, which suggests you're misapprehending vector containers.

When you create a vector container, like this:

vector<char> corrAns;

it has no size. It is of size zero. Attempting to put something into or read something from any element will cause a segfault. If you wish to actually use it, you must increase its size. You can do this with the resize class method. push_back works because the push_back method will automatically resize the vector as needed before adding the data.
Last edited on
Thanks a lot for the detailed and informative post. As I mentioned, I am fairly new to this and this is my first program using vectors, let alone multidimensional vectors. I will heed your advice and try to work out the bugs some more.

Thanks for all your help!!! Mind mentioning how I would go about defending against a segFault?
In simple terms, a segFault is usually caused by trying to read from or write to memory that is in some way not yours. Defend against it by ensuring that you always allocate enough memory for your variables/arrays/containers (i.e. make them big enough), that you never attempt to read off the end of an array or container, and that whenever you dereference a pointer it is definitely pointing where you meant it to point.

Last edited on
Thanks a lot Moschops, you've been a great help!!
Topic archived. No new replies allowed.