Help solving coding issue please.

Alright, so this program compiles just fine, but it doesn't print the name on the commented line. Please help tear this apart.

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<int>::const_iterator iter;  //My variables
	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
	studAns.resize(questions); //Resizing Vectors...
	corrAns.resize(questions); // Because its just good programming procedure

	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
	studName.resize(numStud);  //resize studName vector
	numCorr.resize(numStud);   //resize numCorrect vector
	


	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.push_back(row);  //push back answer into vector
		row.clear();  //call in destructors
	}

	for(int k=0; k<numStud; k++) {  //FOR loop for students correct answers
		string str = studName[k];
		cout << str << " 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
}


Been working on this for a while and there is literally nothing wrong with it that I can tell. Any thoughts, suggestions, or opinions are appreciated. Thanks.

Problem starts after line 57, outputting the correct answers.
Last edited on
// Because its just good programming procedure ¿but why?

reserve <-> push_back
resize <-> subscript
Check the size of your vectors

vector<char> string

A lot of your comments are unnecessary.
I guess the using of reseize and push_back is not exactly correct or the using doesn't reflect what you want to do.

When reseize at #34, the "studName" has a size "1" already.

When push_back at #44, it adds one more element to the current "studName". So the size of "studName" becomes "2". It means, studName[0] is nothing stored, studName[1] is the start of students' name.

#44 looks well, but not works as you want. I think just simply to use "studName[k] = nameStud;"


b2ee
The comments are necessary for my school work, I'm sorry, they aren't very helpful, but I need them for full credit.
Alright, so I have revised the code a bit, but I'm still having an issue. Any suggestions?

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
#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.push_back(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
}
This is respect to the original program you posted and assuming we have 2 questions and 3 students.

This creates a 3x2 array.
 
vector< vector<char> > studentAnswers(numStud, vector<char>(questions));

So later when you do this:
 
studentAnswers.push_back(row);  //push back answer into vector 
You're making a 4x2, 5x2 and then 6x2 array. You should be using:
 
studentAnswers[i] = row;  //push back answer into vector 

You have the same construct in the reposted version.
Alright, I changed that line of code, but it still crashes and I can't figure it out. Any other problems you see? Thanks for the help, I really appreciate it.
Give studName the same treatment.
If you run it under a debugger, when it crashes you'll be able to see which line is causing the crash. Are you using an IDE with an integrated debugger?
Yea, visual studio 2010. I'm a beginner though and I'm not fully familiar with debugging techniques.

I'll check it out. Thanks a lot for the help guys.

@kbw

What do you mean by giving the same treatment to studName?

Instead of:

1
2
3
4
5
for(int i=0; i<numStud; i++) {   
		cout << "Enter the name of student " << i + 1 << ": " << endl; 
		cin >> nameStud;   //cin name of students
		studName.push_back(nameStud);  //instead of this, do   studentAnswers[i] = row ?
		vector<char> row;  
Last edited on
BUMP
BUMP!!
You should consider running your code in a debugger or adding trace statements to see what it's doing.
 
studName.push_back(nameStud);  //push back each name into vector 
should be:
 
studName[i] = nameStud;  //push back each name into vector 
As kbw noted, obviously, my comments didn't make you fully understand where codes should be improved.

Suggestion:
1, have you got C++ reference? Check the method's usage thoroughly before you use it and make sure you exactly understand how it works, such as push_back ( As kbw and me told you).

2, Try to learn using debugger (VC is so bad, your codes can be very easily transformed to linux version, then you can use GDB at this stage), if you school mate don't know how to use debugger, but you can. You could become a code guru in their eyes. :)



b2ee
Last edited on
Topic archived. No new replies allowed.