HandsomeJohn has a good concept, but looping on the condition of "!input.eof()" is a bad idea. This will not work the way you might think. Usually this will process the last read a second time before the while condition figures out that you have reached end of file.
The more accepted way is:
1 2 3 4 5 6
int i = 0;
while (input >> student_array[i].firstName)
{
input >> student_array[i].lastName >> student_array[i].grade;
++i;
}
But even this could be a problem. Consider a first or last name that has a space in the name. "input" like "cin" will read to the first white space or "\n" whichever comes first. This could through the whole read off.
Right now I am working reading a file, but I am only guessing at what the layout of the file looks like. A sample of the "students.txt" file would help.
I am trying to read a line from a file and store that line into a string. Once that line is stored into a string, I need to store it into a struct.
You can use a stringstream to parse the contents of a string into separate variables. I used the same function readLine() as the original code, but changed it to make it work - hopefully as intended.
//stores line from file into a struct
My version attempts to do just that. It reads a line from the file and stores it in a struct. It also return a bool value to indicate whether it succeeded.