how to stop function at end of document

Hey guys. Quick question: How can I edit the getScores function to get it to stop when there are no more values in the data.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
42
43
44
45
#include <iostream>
#include <fstream>
using namespace std;

int getScores(int arr[]);

const int NUM = 100;

int main() {

    int score[NUM];
    int total;
    
    total = getScores(score);
    
    cout << total;
    
    return 0;
}

int getScores(int arr[])
{
    ifstream infile;
    infile.open("/Users/jacobgallipeau/Documents/data.txt");
    
    int test;
    int end = 100;
    int total = 0;
    for(int i=0;i<end;i++)
    {
        infile >> test;
        if(test > 0)
        {
            cout << test << endl;
            arr[i] = test;
            total++;
        }
        else{
            total = end;
        }
    }
    
    infile.close();
    return end;
}
Last edited on
I think you are looking for EOF

http://www.cplusplus.com/reference/ios/ios/eof/

HOPE IT HELPS


EDIT :

http://www.cplusplus.com/forum/general/222711/

Read the answer by Duthomhas its awesome
Last edited on
Topic archived. No new replies allowed.