getting the average within a file

How do you get the average of scores inside a file? I can get it if it's 10 scores, but the assignment says the number of scores can vary and that it would not always be 10.
Whenever I try to modify the average score, it doesn't work.

This is the file with the content in it.
1
2
3
4
5
test_ line_ 10 20 30 40 50 60 70 80 90 100
Price Betty 40 50 60 70 60 50 40 30 60 90
Good John 60 70 80 90 50 60 90 90 100 90
Smith Charles 70 80 90 60 70 60 80 90 90 90
Spangenberg Ward 70 70 80 90 70 80 90 80 70 60


This is my code with average of 10 scores.
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void read_file(ifstream& in_stream, ofstream& out_stream);
void ind_avg(ifstream& in_stream, ofstream& out_stream);

int main()
{
    ifstream in_stream;
    ofstream out_stream;
    read_file(in_stream, out_stream);
    ind_avg(in_stream, out_stream);
}

void read_file(ifstream& in_stream, ofstream& out_stream)
{
    in_stream.open("gradebook.txt");
    if(in_stream.fail())
    {
        cout << "Input file opening failed." << endl;
        exit(1);
    }
    out_stream.open("output.txt");
    if(out_stream.fail())
    {
        cout << "Output file opening failed." << endl;
        exit(1);
    }
}

void ind_avg(ifstream& in_stream, ofstream& out_stream)
{
    int i, sum, a[10];
    string firstname, lastname;
    double averagescore;
    in_stream >> firstname;

    while(in_stream)
    {
        sum = 0;
        in_stream >> lastname;
        for (i=0; i<10; i++)
            in_stream >> a[i];
        sum += a[i];
    }
    out_stream << firstname << " " << lastname << " ";
    for(0; i<10; i++)
        out_stream << a[i] << " ";
    averagescore = sum/10;
    out_stream << averagescore << endl;
}
Read the file line by line, with getline(). Then process each line by reading first name, last name, then do a while loop reading each number. When you process the line you write the first and last name to out_stream. In the while loop over scores, you read the score, write it out, add it to the sum (no array needed), and increment a counter to tell you how many scores you have. Here is some code that you will need to adjust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char somestring[256]
stringstream temp;
double sum,av;
int score,count;
while (in_stream)
{
   in_stream.getline(somestring,256);
   temp<<sometring;
   count=0;
   temp>>firstname>>lastname;
   out_stream << firstname << " " << lastname << " ";
   while(temp>>score)
   {
     sum+=score; count++;
     out_stream << score << " ";
   }
   average=sum/count;
   out_stream << average<< endl;
}
Would I need to adjust the [256]?
That is the maximum length of a line. You can put any number. You have less than 50 in your example
I tried what you did and this is my output file. Also, is there a way to do it without using sstream because we haven't learned it yet.
1
2
3
4
5
6
test_ line_ 10 20 30 40 50 60 70 80 90 100 55
test_ line_ inf
test_ line_ inf
test_ line_ inf
test_ line_ inf
test_ line_ inf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void ind_avg(ifstream& in_stream, ofstream& out_stream)
{
    string firstname, lastname;
    char scores[256];
    stringstream temp;
    double sum, avg;
    int score,count;
    while(in_stream)
    {
        in_stream.getline(scores,256);
        temp << scores;
        count = 0;
        temp >> firstname >> lastname;
        out_stream << firstname << " " << lastname << " ";
        while(temp>>score)
        {
            sum+=score;
            count++;
            out_stream << score << " ";
        }
        avg=sum/count;
        out_stream << avg << endl;
    }
}
I did not test it, but I think you need to move line 5 inside the while loop, as to clear it. Or inside the while loop put temp.str("");
Is it possible to do it using arrays?
Topic archived. No new replies allowed.