What am I doing wrong???

This code compiles but it crashes after inputting answers for the students questions. It will output the first "[studName] has the following questions correct: " and then crashes before outputting anything.

It uses multidimensional vectors which I guess I'm having a bit of trouble understanding. I take it I'm populating the vector incorrectly, or missing something.

Please help, Ive been stuck on this for days.

PS - Lots of unnecessary commenting for school

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
75
76
#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
	numCorr.resize(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
			studentAnswers.push_back(studAns);  // 
		}

		studentAnswers[i] = row;  //
		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.push_back(k);  //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 help everyone!!!
Last edited on
Line 51 sets studentAnswers[i] to row which is empty, so trying to access some element of it causes an error. Why do you think that you need that row vector at all?
I guess I might not, to be honest, I let a friend revise the code, because he thought itd be cool to introduce me to multidimensional arrays. Now, I'm stuck with this by myself trying to debug it and I dont have time to revise it to how I had it.

Thanks for the suggestion. Do you have anymore I could apply to fix this mess?

**EDIT**

Alright, so I got rid of the multidimensional vector, just too many headaches for a beginner like me. Now I think I', back on the right track. How does this look?

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
#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

	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
		 
		
		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
			studAns.push_back(studentAns);  // 
		}

		 
	}

	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 (studAns[l] == corrAns[l]) { //compare
				cout << l << " ";  //cout questions correct
				numCorr.push_back(k);  //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
}


It compiles, but not working quite properly. Any suggestions are appreciated.
Last edited on
Bump
Bump please???
Have patience..

The problem with the numCorr. For every correct answer, you push the id of the student into a vector and later you take the kth value of the vector full of k (this could break, if numStud > questions, by the way). How does that make sense? What you should be doing is incrementing some integer and printing it..

Also, the % thing will always be 0 or 100 because due to integer division 2/3=0. You can do multiplication before division to avoid this tough.
Topic archived. No new replies allowed.