My vector is not getting data from the file I have it retrieve. I created a class called Student, and needed to make a vector out of it to store multiple values for the students. The code worked on my original test file, but when I change the students, it errors out.
Here's the part that's in main:
vector<Student> studentVector; //creating a vector using the defined class Student
string userFile = "";
string filename = "";
int numStudents = 0; //total number of students
int numQuestions = 0; //total number of questions
string key = ""; //answer key
cout << "Enter the name of the file: ";
cin >> userFile; //get name of file from user
filename = checkFile(userFile, numQuestions, key); //gets info from file and returns the new file name to get student answers
fillVector(studentVector, filename, numStudents); //fills vector with values from file
Here's the function that reads the data:
void fillVector(vector<Student>& newStudentVector, string filename, int& numStudents) {
ifstream studentAnswers; //read mode file
string line = ""; //used to read lines
int id = 0;
string fName = ""; //first name
string lName = ""; //last name
string answers = "";
studentAnswers.open(filename); //opens file using filename passed into function
while (getline(studentAnswers,line)) {
++numStudents; //reads the number of lines in file
}
studentAnswers.close(); //closed file because it reached end of file
studentAnswers.open(filename); //reopens file
for (int i = 0; i < numStudents; i++) {
//reads file data
studentAnswers >> id;
studentAnswers >> fName;
studentAnswers >> lName;
studentAnswers >> answers;
Student newStudent(id, (fName + " " + lName), answers, 100.00, "A"); //creates a new object
newStudentVector.push_back(newStudent); //adds new vector with newStudent data
}
studentAnswers.close(); //close file
}