Average line from a text file???

I'm very new to C++ and I have an assignment that has to do with reading in a text file and finding the average of each line. Then pushing the final scores to a vector. I can read in the file but I don't know how to take each line of integers and find the average.

This is the file I am supposed to read in.

79 72 78 71 73 68 74 75 70
89 96 91 94 95 92 88 95 92
81 93 85 84 79 78 90 88 79
100 86 99 98 97 96 95 94 92
60 82 64 65 63 62 61 67 64
92 100 81 82 83 84 85 86 87
91 74 76 77 78 81 83 80 88
60 65 68 72 74 76 77 78 73
99 91 93 94 96 95 97 98 74
81 89 75 77 79 81 83 85 78

How do I take each line of integers from the file and find the average? This is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 void getFileContent(string myLine2){
    string inFileName = "‎lab1.txt";
    ifstream inFile;
    inFile.open(inFileName);

    while (!inFile.is_open()) {
        cout << "Unable to open default file/file path.\nPlease enter a new file/file path:" << endl;
        cin >> inFileName;
        inFile.open(inFileName);
    }
    cout << "FILE IS OPEN!!!\n";
    while(inFile >> myLine2){
        getline(inFile, myLine2);
        cout << myLine2 << endl;
    }

}
Last edited on
You can use a string stream when you have a line read into memory.
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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

void getFileContent(string filename, vector<int> &averages) {
    ifstream infile(filename);
    if ( infile ) {
        string line;
        while ( getline(infile,line) ) {
            istringstream is(line);
            int value;
            while ( is >> value ) {
                cout << "Read " << value << endl;
            }
            cout << "Done with a line" << endl;
        }
    }
}

int main ( ) {
    vector<int> averages;
    getFileContent("foo.txt",averages);
}
This may not be quite perfect, and salem's code looks easy to follow, but I'd already started trying to figure it out, and here's a slightly different way that I did it:

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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int main(){
  int numcount=9;       // the number of values on each line in file

  string myline;

  ifstream file("numbers.txt");

    while (getline(file, myline))
    {
    cout << "Row:\t" << myline << endl;   // the line read from file

    int arr[numcount];
    stringstream ssin(myline);

    int i=0;

       while (ssin.good() && i < numcount){
          ssin >> arr[i];
          ++i;
       }

   float average=0; int total=0;

   for(auto x = 0; x < numcount; x++) total+=arr[x];
   average=(total/numcount);

   cout << "Avg:\t" << average << endl << endl;    // the average
   }

return 0;
}


Row:    79 72 78 71 73 68 74 75 70
Avg:    73

Row:    89 96 91 94 95 92 88 95 92
Avg:    92

Row:    81 93 85 84 79 78 90 88 79
Avg:    84

Row:    100 86 99 98 97 96 95 94 92
Avg:    95

Row:    60 82 64 65 63 62 61 67 64
Avg:    65

Row:    92 100 81 82 83 84 85 86 87
Avg:    86

Row:    91 74 76 77 78 81 83 80 88
Avg:    80

Row:    60 65 68 72 74 76 77 78 73
Avg:    71

Row:    99 91 93 94 96 95 97 98 74
Avg:    93

Row:    81 89 75 77 79 81 83 85 78
Avg:    80

Actually, I can see that salem's version is much simpler, but it was a fun exercise anyway. My modified version below:

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 <string>
#include <sstream>
#include <fstream>

using namespace std;

int main(){
  string myline;
  ifstream file("numbers.txt");

    while (getline(file, myline))
    {
    cout << "Row:\t" << myline << endl;

    stringstream ssin(myline);

    int total=0, ivalue=0, ncount=0;

      while (ssin >> ivalue){
        total+=ivalue;
        ncount++;
      }

    float average=(total/ncount);

    cout << "Avg:\t" << average << endl << endl;
    }

return 0;
}
Last edited on
Topic archived. No new replies allowed.