This program works, but doesn't

So this program compiles and runs, but when it outputs the number and percentage correct, it outputs the same results for each different student, and it does print the correct name for each loop but the same results regardless of what they got. I don't understand why but I'm a beginner.

Any help is much appreciated!!!

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
#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;                 //
	double questions = 0;               //
	double numCorrect = 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);  // 
		}

		 
	} //It would seem the problem starts somewhere in here, unless I'm not populating one of my vectors correctly?

	for(int k=0; k<numStud; k++) {  //FOR loop for students correct answers
	
		cout << studName[k] << " was correct on the following questions: " << endl; //cout correct?

		for(int l=0; l<questions; l++){  //FOR loop to compare and output ans
			if (studAns[l] == corrAns[l]) { //compare
				cout << " Question " << l+1 << ", ";  //cout questions correct
				numCorrect += 1;  //counter
			}

		}
		cout << endl;  //spacer
		cout << "for a total of " << numCorrect << " answers." << endl; //total correct answers
		cout << "And a percentage of " << ((numCorrect/questions) * 100) << "%." << endl << endl;  //percentage correct
		numCorrect = 0;


	}

	system("pause"); //system pause
	return 0; //return
}
Last edited on
Lines 39 to 43...that pushes all the answers from all the students into the same vector. But then later, on lines 52 to 56...you only read the answers from the first student. You should offset by k * questions, eg
if(studAns[l + k * questions] == corrAns[l])
This way, you get the set of answers that goes with the student.
Last edited on
Oh wow, i can't believe I didn't see that. I've even been writing it back down in pseudocode to see if I was missing something.

Thanks a lot LB, you might just be my hero!!
closed account (D80DSL3A)
You are storing all of the students answers in the same vector but this is OK, you can make it work!
Looking at lines 33-43:
1
2
3
4
5
6
7
8
9
10
11
12
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);  // 
		}

Stripping out all code except the pushing of the answers may help you to see my point:
1
2
3
4
5
for(int i=0; i<numStud; i++) {		
		for(int j=0; j<questions; j++) {			
			cin >> studentAns;   //cin answer
			studAns.push_back(studentAns);// all students answers are pushed into this vector 
		}

To see this, add this code (temporarily) after line 43:
cout << "# of stored answers = " << studAns.size() << endl;
I expect you will see a number = numStud*questions.
So, student 0's answers are stored in studAns[0] through studAns[questions-1].
student 1's answers are stored in studAns[questions] through studAns[2*questions-1].
...
student k's answers are stored in studAns[k*questions] through studAns[(k+1)*questions-1].

The fix? Change line 53 from if (studAns[l] == corrAns[l]) { //compare
to if (studAns[k*questions+l] == corrAns[l]) { //offset to student k's answers
Let me know if that works! Hopefully I didn't miss something else.
You did a great job fun2code, and I understood your answer clearly.

Unfortunately, LB answered it first, but your answer was much more in-depth. Thanks a lot for the very detailed and clear answer, I'm a beginner and I like to have it described so that I may learn it properly, rather than just being given the answer.

Thanks a lot fun2code. Great post!!
closed account (D80DSL3A)
Whoops, I see that! Apparently I poked around with preparing my answer for over 1/2 hour! Meanwhile...the world moved on.
I think LB's answer, while shorter, was very concise and to the point.
I'll try to be quicker next time.
Topic archived. No new replies allowed.