Averaging outputs of each execution of a file The only way I could think of doing this is creating a text file and modifying it each time the program is executed and outputting the average but this has many flaws so I was wondering if it was possible to do this any other way.

int num1, num2, counter = 0;
num2 = rand();
do {
cin &gt;&gt; num1;
counter++;
} while(num1 != num2)

const char *path = "C:/Users/Edsheeran/AppData/Roaming/times.txt";
double x = 0;
double sum = 0;
vector&lt;double&gt; times;

ofstream write(path, ios::app);
if(write.is_open()) {
write &lt;&lt; counter &lt;&lt; endl;
};
write.close();

ifstream file(path, ios::app);
if(file.is_open())
{
    while (!file.eof()) {
        file &gt;&gt; x;
        times.push_back(x);
    }
}
else exit(EXIT_FAILURE);
file.close();

for (unsigned int i = 0 ; i &lt; times.size(); i++) {
sum += times[i];
}

cout &lt;&lt; "Your average tries to find the number is: " &lt;&lt; sum / times.size()   &lt;&lt; endl;
I think the short answer to your question is "what you're doing is fine, but there are other approaches depending on what you want to do." However, first:


When posting on CodeReview, try to post the most complete, compilable code that you can. This is often less work for you (because you can just paste straight from your source file with no editing), and less work for the reviewers (because they can just paste it straight into a source file and compile it). What you've posted here seems to be just the body of main.
Please indent your code consistently. This is good(ish):

if(file.is_open())
{
    while (!file.eof()) {
        file &gt;&gt; x;
        times.push_back(x);
    }
}


but this is bad:

if(write.is_open()) {
write &lt;&lt; counter &lt;&lt; endl;
};


You can use a tool like clang-format, astyle, or indent to format your code automatically until you acquire the muscle memory to do it yourself.




However, both of those snippets I just pasted have formatting "bugs" besides the indentation:  In the second one, you have a trailing ; that shouldn't be there.  In the first one, you mix brace styles:

if (x)
{


and

while(x) {


(Notice the mix of pre-parenthesis-spacing styles as well.)  Pick one style for a given program and stick with it.



Lastly, try to structure your program by breaking it down into subroutines. For this program, I'd probably have subroutines

// Generate a random number and return the number of tries for the user to guess it.
int guess_the_number();

// Append a number to the named file.
void append_to_file(const char *filename, int value);

// Retrieve the contents of the named file.
std::vector&lt;int&gt; load_ints_from_file(const char *filename);


And then I'd strongly consider refactoring the program along the lines suggested by symmetry: namely, would it make more sense to provide a function

// Retrieve the contents of the named file.
void save_ints_to_file(const char *filename, const std::vector&lt;int&gt;&amp; values);


instead of the append_to_file function?  There are valid reasons you might not want to in production code (for example, the append-only version has better efficiency and atomicity, if you do it right — which is not to say that you have), but if your goal is just to make a readable and sensible toy guess-the-number program, personally I'd go with the load/save dichotomy in place of the load/append dichotomy.



As for your question about "how to keep a running average": if all you care about is the average number of guesses (i.e. total number of guesses divided by total number of games played), then you don't need to keep a record of how the total number of guesses happened to divide up into individual games-played. Just store those two numbers in your file:

std::vector&lt;int&gt; values = load_ints_from_file("times.txt");
if (values.empty()) values = std::vector&lt;int&gt;{0, 0};
values[0] += guess_the_number();
values[1] += 1;
std::cout &lt;&lt; "Average is " &lt;&lt; values[0] / values[1] &lt;&lt; std::endl;
save_ints_in_file("times.txt", values);