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