file input counting problem

the following code is part of a program i am working on. i want to use the stu_count variable to find averages later in the program. it counts to 13 when there is 12 lines in the file and should output 11 any one know why?
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

short stu_count = 0;

int main()
{
    ifstream inData;
    inData.open ("prog5data.txt");

    cout << fixed << showpoint; //sets decimals to certain place

    string last_name[40], first_name[40];
    float scores[40][8];
    int ID[40];

    for (;inData; stu_count++)
    {
        inData >> last_name[stu_count] >> first_name[stu_count] >> ID[stu_count];

        for (short col = 0; col <= 7; col++)
            inData >> scores[stu_count][col];
    }
    inData.close ();

    cout << stu_count;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Text file "prog5data.txt"

Lee, Freddy    38736 74.1 83.3 73.6 43.7 53.0 64.5 57.9 49.1
Griswold, Ed   38549 57.4 88.4 54.0 97.1 49.5 78.4 91.3 39.4
Romulus, Al    48265 54.8 58.4 94.6 74.8 84.0 67.7 84.6 79.8
Forest, Mike   72736 74.8 73.0 86.8 57.6 00.0 46.8 73.6 58.3
Kurawski, Ole  83737 65.7 64.9 45.9 65.4 33.9 74.9 47.9 78.0
Crawford, Dee  44847 57.6 75.4 87.4 76.4 44.4 75.6 57.7 67.0
William, Ray   43849 55.8 75.0 45.7 78.7 75.4 45.7 49.6 85.7
Lozini, Femeo  47368 84.7 74.8 44.9 75.3 84.3 77.3 87.3 44.8
Lawson, Sandy  84584 85.3 74.9 78.3 66.0 37.5 82.4 79.6 75.3
Levison, John  49403 44.3 84.9 43.7 84.4 43.9 74.6 74.0 74.2
Ferry, Larry   36458 88.1 83.7 73.0 78.3 69.7 74.5 66.9 73.9
Foly, Joseph   60373 74.9 33.8 47.7 65.9 74.0 78.8 48.4 69.3


Last edited on
It should count to 12, not 11. Probably counting to 13 because your file has a blank line at the end?
it doesnt have a blank line at end but still counting to 13 any ideas why
It's some garbage at the end of the file. It get picked up because of your loop test for (;inData; stu_count++).
If you change your test to for (; !(inData.eof()); stu_count++) then
all is well.

Topic archived. No new replies allowed.