Adding in while loops

Hi, I need help with a program. I am new to programing. I need to read from a file and output to a file. I am able to do that. But I am having trouble when it comes to outputting the average total time (b+c+d). It only does the average of the last number. I want to add all the numbers in total time and average those. Thanks for the help.
This is the input file.
A 31 27 52
B 29 31 58
C 35 36 51
D 34 36 62

This is the output file im getting.
Running Swimming Biking Total Time
A 31 27 52 110
B 29 31 58 118
C 35 36 51 122
D 34 36 62 132

Average Total Time: 33



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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main ()
{
    
    ifstream infile;
    ofstream outfile;
    char a;double b;double c;double d;
    
    
    infile.open("racedat.txt");
    outfile.open("outracedat.txt");
    
    if (infile.fail()){
        cerr << "Error Opening File" <<endl;
        exit (1);
    }
    
    while(infile){
        outfile << setw(9) << "Running" << setw(10) << "Swimming" << setw(8) << "Biking" <<setw(12) << "Total Time"<<endl;
        
        while(infile>>a>>b>>c>>d)
            outfile << a << setw(5) << b << setw(9) << c << setw(10) << d << setw(9)<<(b+c+d) <<endl;
        
        outfile <<endl;
        outfile << "Average Total Time: " << (b+c+d)/4 <<endl;
               
   }
  
    return 0;
}

You are looking for this concept:
// initial condition
sum = 0
count = 0

// Repeating:
for each element
  sum += something about the element
  ++count

// after N elements the count==N and N values have been added to the sum

// average (unless count==0)
double average = static_cast<double>(sum) / count;
can you please clarify, i have no idea what you mean.
Topic archived. No new replies allowed.