.eof() problem in array

the loop does not work with the .eof() and i was wondering why not. it doesnt print anything. my program has to be able to find the averages of students and the number of students has to be able to change in txt file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
    ifstream inData;
    string LstName[40], FstName[40];
    int ID[40], stu = 0;
    float scores[40][8];

    inData.open ("prog5data.txt");


    while (inData)
    {
        inData >> LstName[stu] >> FstName[stu] >> ID[stu];

        for (short i=0; i<=7; i++)
            inData >> scores [stu][i];

        stu++;
    }

    float sum;
    for (int stu=0; !inData.eof() ;stu++)
    {
        sum = 0;

        for (int i=0;i<=7;i++)
            sum += scores[stu][i];

        cout << sum / 8 << endl;
    }


    inData.close ();
    cin.get();
    return 0;
}



txt file
Lozini, Femeo 322838549 57.4 88.4 54.0 97.1 49.5 78.4 91.3 39.4
Romulus, Mary 419548265 54.8 58.4 94.6 74.8 84.0 67.7 84.6 79.8
Forest, Mike 572872736 74.8 73.0 86.8 57.6 00.0 46.8 73.6 58.3
Kurawski, Ole 438483737 65.7 64.9 45.9 65.4 33.9 74.9 47.9 78.0
It will never signal EOF because you never try to read from the file in your for loop.

BUT, the for loop will actually never execute, because the file is already at EOF after you exit the while loop.

Lines 14-22 read the file's data into your arrays. You might as well just close the file on line 23 and be done with it.

Then on lines 24-33 just count through your arrays. You already know how many students there are (by line 23 stu is the number of students), so you can use that to know when to stop.


Thoughts for the future: What happens if there are more than 40 students in the file?

Hope this helps.
Topic archived. No new replies allowed.