End of File not working Properly

This is a piece of a large program that im making. It is supposed to read names and grade from a file into arrays. However, it keeps running through all 100 loops as if the file never ends. It reads the first name and grades but none of the others. Here is the code.

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
42
43
44
#include<iostream>
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
int main(){
    const int numstudents = 100;
    const int numquiz = 5;
    const int numtest = 2;
    const int numassignment = 4;
    const int numletters = 30;
    int tests[numstudents][numtest];
    int quizzes[numstudents][numquiz];
    int assignments[numstudents][numassignment];
    char names[numstudents][numletters];
    char buf[30];
    std::ifstream infile;
    infile.open("gradebook.txt");
    for(int i = 0; i < 100; i++){
        infile.getline(buf, 30);
        std::cout<<"hello ";
        if(!infile.eof()){
            strcpy(names[i],buf);
            for(int q = 0; q < 5; q++){
                infile>>quizzes[i][q];
            }
            for(int t = 0; t < 2; t++){
                infile>>tests[i][t];
            }
            for(int a = 0; a <4; a++){
                infile>>assignments[i][a];
            }
        }
        else{
            i = 101;
        }
    }
    infile.close();
    std::cout<<names[0]<<std::endl;
    std::cout<<names[1]<<std::endl;
    std::cout<<tests[1][0]<<std::endl;
    std::cout<<tests[0][0];
    return 0;
}



Here is the file it is reading.

william wade
80
90
50
100
70
100
100
70
80
90
100
anna claire
100
90
80
70
80
100
90
90
80
70
50


And this is what it prints.

hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
william wade

36
100

It prints off 100 "hello" s and it prints off the correct first name and the grade from the first person, but it doesn't print off the second name or the grade correctly.
just use while(not infile.eof()) there is no need to run the for loop 100 times
Whenever i try that with this code:


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
42
#include<iostream>
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
int main(){
    const int numstudents = 100;
    const int numquiz = 5;
    const int numtest = 2;
    const int numassignment = 4;
    const int numletters = 30;
    int tests[numstudents][numtest];
    int quizzes[numstudents][numquiz];
    int assignments[numstudents][numassignment];
    char names[numstudents][numletters];
    char buf[30];
    int howmany = 0;
    std::ifstream infile;
    infile.open("gradebook.txt");
    while(!infile.eof()){
        infile.getline(buf, 30);
        std::cout<<"hello ";
        strcpy(names[howmany],buf);
        for(int q = 0; q < 5; q++){
            infile>>quizzes[howmany][q];
        }
        for(int t = 0; t < 2; t++){
            infile>>tests[howmany][t];
        }
        for(int a = 0; a < 4; a++){
           infile>>assignments[howmany][a];
        }
        howmany++;
    }
    infile.close();
    std::cout<<"close"<<std::endl;
    std::cout<<names[0]<<std::endl;
    std::cout<<names[1]<<std::endl;
    std::cout<<tests[1][0]<<std::endl;
    std::cout<<tests[0][0];
    return 0;
}





It gives me this:


hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
hello hello hello



It loops 403 times and doesn't store or print the arrays or the close at the end of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    while(infile.getline(buf, 30)){
        std::cout<<"hello ";
        strcpy(names[howmany],buf);
        for(int q = 0; q < 5; q++){
            infile>>quizzes[howmany][q];
        }
        for(int t = 0; t < 2; t++){
            infile>>tests[howmany][t];
        }
        for(int a = 0; a < 4; a++){
           infile>>assignments[howmany][a];
        }
        howmany++;
        infile.ignore() ;
    }
Thank you so much. It works now.
Topic archived. No new replies allowed.