0

The file im dealing with for input looks like:

Student Name

GPA

Student Name

GPA

etc..

Ive tried using getline followed by fin (my file input command) but i keep getting errors.

I cannot use vectors as i must use getline. I am allowed using fin.ignore() among other but not sure what to do.

while (!fin.eof()) {
    getline(fin, studentNames[i]);
    fin >> studentGPA[i];
    cout << studentNames[i] << endl;
    cout << studentGPA[i] << endl;
    ++i;
    cout << numberOfStudents << endl;
    ++numberOfStudents;
}
  • You should use regex or something to validate the file line, then if it matches a certain pattern, (i.e. is a float) use `std::atoi` or something. Also seems like a pretty stupid and inefficient way to store data in a file. – Alexander Kleinhans Apr 07 '18 at 01:18
  • What errors do you get ??? – Shadi Apr 07 '18 at 01:20
  • 1
    [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong?s=1|135.7997) – O'Neil Apr 07 '18 at 01:26
  • This is what i changed the code to but i want it to continue with ther other names and GPA of the other students. while (getline(fin, studentNames[i])) { cout << studentNames[i] << endl; fin >> studentGPA[i]; fin.ignore(SIZE_MAX, '/n'); cout << studentGPA[i] << endl; ++i; cout << numberOfStudents << endl << endl; ++numberOfStudents; } – Spencer Hudson Apr 07 '18 at 02:43
  • Read a line; store it as text; read a line; convert it to a number and store it; repeat until done. – Pete Becker Apr 07 '18 at 14:04

0 Answers0