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