-1

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
}
Derek H
  • 31
  • 7
  • 1
    Why do you need to open the file twice? Why not open it once and read the students the first time? You will know how many students by `newStudentVector.size()`. – PaulMcKenzie Dec 16 '16 at 03:11
  • You're right, I didn't even think about that! – Derek H Dec 16 '16 at 03:15
  • Search the internet for "stackoverflow c++ read file vector". Always research first. – Thomas Matthews Dec 16 '16 at 03:31
  • @ThomasMatthews I already did my research and have not found an answer to my question. My question is why is the vector getting no data with new testing when the first test it gave me data? – Derek H Dec 16 '16 at 04:14
  • How do you know (and how can we know) that the vector is getting no data? I don't see you testing or displaying it anywhere. Please reduce your original program to the smallest possible **complete** program that demonstrates the problem. Then [edit] your question to copy-paste that short, complete program into your question. Please include the actual and expected output of your program. See [mcve] for more information. – Robᵩ Dec 16 '16 at 04:36

2 Answers2

0

You have to open your string-name file like this :

studentAnswers.open(filename.c_str());

Try to loop through your vector like this :

getline(studentAnswers,line)
while (!studentAnswers.eof()) { 
    getline(studentAnswers,line)
    ++numStudents;
}
YouneS
  • 390
  • 4
  • 10
  • since C++11, a std::ifstream can be opened using const std::string& filename) – 2785528 Dec 16 '16 at 04:05
  • 1
    Please don't use `.eof()` as a while-loop condition. It almost always produces buggy code, as it did in this instance. See http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line – Robᵩ Dec 16 '16 at 04:35
0

Assuming that you've implemented operator>>(std::istream&, Student&), then a fairly easy way to implement fillVector() is to use stream iterators.

void fillVector(std::vector<Student>& newStudentVector, 
                std::string filename, int& numStudents) {
    std::ifstream studentAnswers(filename);
    if (!studentAnswers) {
        std::cout << "WARNING! studentAnswers file not found\n";
    }
    newStudentVector.assign(
        std::istream_iterator<Student>(studentAnswers),
        std::istream_iterator<Student>());
    numStudents = newStudentVector.size();
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308