Help with crashing program...

Hey all, I am not yet finished with this program, but as it is, it crashes and I want to fix what is wrong with it before moving on. I maybe way wrong on this as I am a beginner and this is homework. Not looking for answers, just help understanding. Thanks.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;


int main()
{
	vector< int >::const_iterator iter;
	vector<string> studName;
	vector<char> corrAns;
	vector<char> studAns;
	vector<int> numCorr;
	char correctAns;
	string nameStud;
	int numStud;
	int questions;
	int x = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	
	cout << "\n How many questions will be tested? " << endl; 
	cin >> questions;

	for( int x=0; x<questions; x++){

		cout << " Enter the answer for question " << x+1 << ": " << endl; //enter correct answers
		cout << " A, B, C, or D? " << endl;
		cin >> correctAns;
		corrAns.push_back(correctAns);
	}


	cout << "\n Enter the number of students to test: " << endl;  //how many students will be tested?
	cin >> numStud;

	for( int i=0; i<numStud; i++){ //For loop for student names, per num of students

		cout << endl;
		cout << " Enter the name of student " << i+1 << ": " << endl;
		cin >> nameStud;
		studName.push_back(nameStud);

		for( int j=0; j<numStud; j++){  //to get answers from each student by name

			cout << endl;
			cout << "Enter the answer for " << studName[j+1] << " : " << endl;
			cin >> correctAns;
			studAns.push_back(correctAns);
		}

	for( int k=0; k<questions; k++){  //To see if they had the correct answers, will have to implement correct percentage as well.

		if( corrAns[k] != studAns[k])
			
			cout << endl;
			cout << studName[k] << " was wrong on question " << k+1 << ". " << endl;
	}

	}


	return 0;
}


Compiles fine but crashes after inputting the first name of student. Am I nesting the FOR loops correctly?
I have yet to implement a section to calculate percentage correct and outputting the student names and corresponding score descending.

Thanks again for any help, thoughts, suggestions, and opinons.
Last edited on
cout << "Enter the answer for " << studName[j+1] << " : " << endl;

Why is this j+1 rather than j? What happens if studName is of size 1? It will segfault when you try to get studName[1], because the first element is studName[0].

If you ran your code under a debugger, it would have identified this as the line where the segfault occurs.
Last edited on
Yea, I already fixed that. Been working on it since nobody was replying.

Thanks though.

Now, I'm having a problem with commented section of code:

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
int main()
{
	vector< int >::const_iterator iter;
	vector<string> studName;
	vector<char> corrAns;
	vector<char> studAns;
	vector<int> numCorr;
	char correctAns;
	char studentAns;
	string nameStud;
	int numStud = 0;
	int questions = 0;
	int x = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	
	cout << "\n How many questions will be tested? " << endl;
	cin >> questions;

	for( int x=0; x<questions; x++){

		cout << " Enter the answer for question " << x+1 << ": " << endl;
		cout << " A, B, C, or D? " << endl;
		cin >> correctAns;
		corrAns.push_back(correctAns);
	}


	cout << "\n Enter the number of students to test: " << endl;
	cin >> numStud;

	for( int i=0; i<numStud; i++){

		cout << endl;
		cout << " Enter the name of student " << i+1 << ": " << endl;
		cin >> nameStud;
		studName.push_back(nameStud);
	
		
	for( int j=0; j<questions; j++){

		cout << endl;
		cout << "Enter the answer for question " << j+1 << " : " << endl;
		cin >> studentAns;
		studAns.push_back(studentAns);
		}
	
//It crashes after one pass

//	for( int k=0; k<numStud; k++){

//		if( corrAns[k] != studAns[k] )
			
//			cout << endl;
//			cout << studName[k] << " was wrong on question " << k+1 << ". " << endl;
	}

	}
Last edited on
bump
Exact same problem. You are trying to access part of studName which does not exist.

Your code gets one student name, then one student answer, then loops from 0 to k (i.e. the number of students), but at this point you've only put one student name in there so studName is of size one.

Again, if you ran this under a debugger, you would have found this yourself.
Last edited on
I'm a beginner. I thought I clarified this?

And yet, I don't understand why the last FOR loop shouldn't work if I previously populated each vector in use, shouldn't "k" point to each element in vector through each iteration? Should I test each student in seperate FOR loops?

**EDIT**

Well, I have recognized the issue after further examination, but can anybody help guide me to a reasonable solution?
Last edited on
bump

I'm a beginner. I thought I clarified this?


Perhaps a better question to ask is "How do I use this debugger of which you speak?", rather than making sure than we all know you're not very experienced. We don't care if you're a beginner, but watching you repeat the same mistake in the space of a few posts is not fun.

And yet, I don't understand why the last FOR loop shouldn't work if I previously populated each vector in use,

You haven't populated it. You are putting one student name in, and then entering the next for loop, and then you're getting one answer, and entering the next for loop, and then in that third for loop,

for( int k=0; k<numStud; k++){

you are reading studName[k] for k=0, which is fine because you put something in studName[0], and then you are reading studName[k] for k=1, which is not fine because you have not yet read in the second student name.

EDIT:
Well, I have recognized the issue after further examination, but can anybody help guide me to a reasonable solution?


Yes. Restructure programme like this:

1
2
3
4
5
// get all student names first.

// then get all answers for each student

// then check all answers 


Last edited on
@ Moschops

Well, thanks a lot for your help and I will go about working on that now.

Thanks for sticking with me and being patient. I do apologize for the ignorance, it's just been frustrating the hell out of me.
Topic archived. No new replies allowed.