#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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.
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;"
#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
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
}
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.
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;
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. :)