Im having trouble with this assignment that has me write vectors and arrays to a file

So in this assignment I have to have one function that has the user enter the vector which is the driver name and then they enter the drivers 4 points. That function is fine. I also Have to do a second function that writes the name and the average that I get from the 4 points to the txt file. and then display the name and averages in main. Right now I am stuck on the second function because I'm having trouble getting it to where it would write the vector and then the average after it. right now it is writing both vector elements and then the first average, and then both vector elements and the second average. can someone help me

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include <fstream>
using namespace std;

//function prototypes
void Enter_Driver_Data(vector<string> &driverName, int points[8][4], string fileName, fstream& file, int& counter);
void Calculate_Average(vector<string> &driverName, int points[8][4], string fileName, fstream& file, int& counter);

int main()
{
    int counter = 0;//this counter is for the average function
    int points[8][4];
    vector<string>driverName;
    fstream file;
    string fileName;
    int average;


    //user will enter a text file name

    cout << "Please enter the file name: ";
    getline(cin, fileName);

    Enter_Driver_Data(driverName, points, fileName, file, counter);
    Calculate_Average(driverName, points, fileName, file, counter);
    cout << setprecision(1) << fixed << endl;

    fstream txtInput(fileName, ios::in);
    //outputting the txt file data
    for (string i : driverName)
    {
        cout << i << "'s average points is: ";
        txtInput >> average;
        cout << endl;

    }
    

    
    cout << endl << endl;
    system("pause");
    return 0;
}
void Enter_Driver_Data(vector<string> &driverName, int points[8][4], string fileName, fstream& file, int& counter)//this function is for entering data
{
    file.open(fileName, ios::out);

    string driverNameInput;
    //filestream declarations
    fstream txtOutput(fileName, ios::out);
    

    //again declaration
    bool again = true;
    //for loop for data
    for (int name = 0; name < 8 && again; name++)//name is for the row of the array
    {
        cout << "\nPlease enter the driver's name: ";
        getline(cin, driverNameInput);

        driverName.push_back(driverNameInput);
        txtOutput << driverName[name] << ": ";

        for (int score = 0; score < 4; score++)//score is for the column of the array
        {

            cout << "\nPlease enter " << driverName[name] << "'s score number " << score + 1 << ": ";
            cin >> points[name][score];
            while (points[name][score] != 60 && points[name][score] != 70 && points[name][score] != 80 && points[name][score] != 90 && points[name][score] != 100)
            {
                cout << "Your input was invalid: please enter the scores which are 60, 70, 80, 90,or 100: ";
                cin >> points[name][score];
            }
            cout << points[name][score] << endl;
            txtOutput << points[name][score] << " ";
            
            cin.ignore();
            cin.clear();
        }
        txtOutput << endl;
        cout << "Would you like to enter another driver?(1 for yes and 0 for no): ";
        cin >> again;
        cin.ignore();
        cin.clear();
        counter++;
        
    }
    file.close();
}
void Calculate_Average(vector<string> &driverName, int points[8][4], string fileName, fstream& file, int& counter)
{
    file.open(fileName, ios::app);
    fstream txtOutput(fileName, ios::app);
    double totalBeforeAverage, average;
    vector <double> avg;
    for (int name = 0; name < counter; name++)
    {
        totalBeforeAverage = 0, average = 0;

        for (int score = 0; score < 4; score++)
        {
            totalBeforeAverage += points[name][score];
        }
        average = totalBeforeAverage / 4;
        avg.push_back(average);
    }
    for (string drivName : driverName)
    {
        txtOutput << drivName << ": ";

    }
    for (double averageToFile : avg)
    {
        txtOutput << averageToFile << endl;
    }
    file.close();
    
}

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum