getline not reading correctly

Hello all! Working on a project where I need to fill a file with numbers and use a getline to read those numbers line by line and then display the total, average, max, and min from each line. Everything's in place except my getline doesn't seem to be working as the output for the total, average, etc, is always 0. Any help at all is appreciated. Thanks!
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ofstream myfile;
    myfile.open("chapter_test.txt");
    if(!myfile)
    {
        cout << "Unable to open file\n";
        return 0;
    }
    if(myfile.is_open())
    {
        myfile << "Numbers: \n";
        myfile << "90 63 84 52 21 93 77 46\n";
        myfile << "90 22 26 34 39 44 75 98\n";
        myfile << "28 28 85 57 28 33 66 100\n";
        myfile << "16 80 74 62 42 84 42 56\n";
        myfile << "85 44 76 97 16 64 80 14\n";
        myfile << "41 85 13 88 78 8 18 38\n";
        myfile << "53 49 71 79 75 57 93 62\n";

        fstream infile;
        infile.open("chapter_test.txt");
        int total, average, max=0, min=0, num;
        while(!infile.eof())
            {
                string line;
                getline(infile, line);
                int a, b, c, d, e, f, g, h, i;
                infile >> a >> b >> c >> d >> e >> f >> g >> h >> i;
                total = a+b+c+d+e+f+g+h+i;
                average = total/7;
                while(infile>>num)
                    {
                        max = num;
                        min = num;
                        if(max<num)
                            {
                                max = num;
                            }
                        if(min > num)
                            {
                                min = num;
                            }
                    }
                myfile << "                               TOTAL        AVERAGE      MAX      MIN\n";
                myfile << "90 63 84 52 21 93 77 46    " << total << "  " << average << "  " << max << "  " << min << endl;
                myfile << "90 22 26 34 39 44 75 98\n";
                myfile << "28 28 85 57 28 33 66 100\n";
                myfile << "16 80 74 62 42 84 42 56\n";
                myfile << "85 44 76 97 16 64 80 14\n";
                myfile << "41 85 13 88 78 8 18 38\n";
                myfile << "53 49 71 79 75 57 93 62\n";
                
            }
        infile.close();
    }
    myfile.close();
    return 0;
}
A few comments.

The test at line 14 if(myfile.is_open()) is redundant, since the program would have already ended at line 12 if it was not.

At line 61, myfile.close(); just before the program ends is logically out of sequence. You need to close the output file before attempting to re-open it for input. (well, there may be exceptions, but let's keep things simple).

There is no check that the input file was opened successfully.

Using eof() in a loop condition is a no-no, don't do it or things will usually turn out badly. What you do need to do is try to read a line from the file, and only process that line if the read operation was successful. eof() before the read doesn't tell you that.


One more thing - you may not be familiar with this. To get the numbers contained within a line of text, the easiest way is to use a stringstream, and read integers from that as though it was a 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
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main() 
{
    ofstream myfile("chapter_test.txt");
    if (!myfile)
    {
        cout << "Unable to open output file\n";
        return 1;
    }
    
    myfile << "Numbers: \n"
              "90 63 84 52 21 93 77 46\n"
              "90 22 26 34 39 44 75 98\n"
              "28 28 85 57 28 33 66 100\n"
              "16 80 74 62 42 84 42 56\n"
              "85 44 76 97 16 64 80 14\n"
              "41 85 13 88 78 8 18 38\n"
              "53 49 71 79 75 57 93 62\n";
              
    myfile.close(); // close the file before trying to read from it.
    

    ifstream infile("chapter_test.txt");
    if (!infile)
    {
        cout << "Unable to open input file\n";
        return 2;
    }

    // Read the file line by line
    string line;        
    while ( getline(infile, line) )
    {
        // read each integer in the line
        
        istringstream ss(line);
        int n;
        
        while (ss >> n)
        {
            cout << setw(4) << n; 
            // add whatever other processing you need here.
        }
        cout << '\n';
    }

}
Topic archived. No new replies allowed.