numeric formatted input (I assume GPA is a number), performed by
>> sList[cint].GPA
, is required to set eofbit if the last character in the file is a digit, and not set eofbit if there was something else after that character (e.g. whitespace, in your case).
To consume whitespace from the input file, you can use the I/O manipulator std::ws, like so (I also fixed your code duplication)
1 2 3 4 5 6 7 8 9 10
|
cnt = 0;
while(cnt < MAX_STUDENTS && inFile >> sList[cnt].ID >> sList[cnt].hours >> sList[cnt].GPA >> std::ws)
{
++cnt;
}
if(!inFile.eof() && cnt == MAX_STUDENTS)
{
std::cout << std::setw(4) << "\tI can only store " << MAX_STUDENTS << " items in each struct type.\n\n";
}
|
this only prints the message if after processing MAX_STUDENTS entries, there is something that's not whitespace in the file.
ways to improve:
Use vectors
Give your struct an operator>> so it becomes inFile >> sList[cnt]
If the input format is three numbers per line, then use line-oriented input (while(getline(inFile, line), and parse that, instead of allowing free-flow as it does now.