### Freaking Functions

A function of mine reads as follows:

 ``123456789101112131415`` ``````int number, count = 0; double, sum = 0; fileName >> number; sum = sum + number; count++; while (!fileName.eof()) { fileName >> number; sum = sum + number; count++; } cout << "Average = " << sum/count << endl; cout << endl; fileName.close();``````

Now, how in the world does this compute the average of the following text file that reads:
 5 7 8 4 2 9 4 6 3 7 4 5 7 2 1 4 4 7 8 9 4 5 7 6 3 1 5

"Hold my hand" step by step, please. I'm still a neophyte in the world of C++.
;__;
I see it reads the first number in the file (5)
Then it does some math, changing the value of sum (sum = sum + number).
This will make it 5 again.
Then it increments Count by one, making it 1.
But how does it get to the next line of the file and, eventually, to the bottom/end?

Last edited on
Ok, you wanted step by step, so I'll go line by line:
 ``1234567891011121314151617181920212223242526272829303132`` ``````// Creates two variables of type int - positive or negative whole numbers int number, count = 0; // I believe there is an error here with the comma // Should create a variable of double precision floating point (a decimal) double, sum = 0; // Reads an integer from the fileName object and stores it in number fileName >> number; // Takes the sum and sets it equal to the new number and the old sum sum = sum + number; // Increments count (the variable that tells you how many numbers there are count++; // The start of a while loop // Checks to make sure the end of file hasn't been reached on the fileName object while (!fileName.eof()) { // Reads another number into the variable number from fileName fileName >> number; // Adds the sum and number together again and assigns it to sum sum = sum + number; // Increases the count count++; } // The while loop will continue as long as there are more numbers // Displays "Average = " and then the value of sum divided by count // Since sum was a double, it will return a value with double precision // The displays an end of line character/carriage return (depends on your OS) cout << "Average = " << sum/count << endl; // Displays another EOL/CR character cout << endl; // Closes the file fileName.close();``````

There is a few errors in function itself, I'm guessing either failed copy of it or whatever. It can also be shortened up a little using some of the simple shortcuts of C++ such as the `+=` operator which adds whatever is on the right to whatever is on the left and assigns it to the left variable. A do/while loop would also work a little better in this situation. And if for whatever reason there is no values in the file, you'll get a runtime error, I believe, since you'd be dividing by 0.

 But how does it get to the next line of the file and, eventually, to the bottom/end?

This, my friend, is some of the magic of the fstream objects. It moves how you tell it to, even if you don't know you're doing such a thing. By default, the fstream object ignores whitespace, spaces, line returns, etc. and only reads out what it is told to, in this case, an int.

When it reaches the end of a file, it sets a flag. The while loop is checking that flag on each loop by calling `fileName.eof()`. If the EOF is reached, the function will return true. Since the while statement has a !, not equal to, as long as .eof() is false, it will continue to run. Once it's true, it will break the loop.
Last edited on
I know you probably know how loops work by now so we will start there.
It is true that it will go into the loop and read 5 first and add 5(num) to 0(sum).
This will then assign 5 to sum. it will then start your counter at 1.
The while loop is set to keep going until it has reached the end of the infile. that's what eof stands for.
then it will move on to the next num(7) and add it to sum(5) and replacing sum with 12. it will keep doing this as long as it has to until the end of the file.
The following portion of your code while (!fileName.eof()) says "i will keep doing the following as long as I have not reached the end of the file". once it reaches the end of the file the next lines of code will take the int sum (which is the total of all the numbers in the infile) and divide it by the total amount of integers in the infile(this is what the counter keeps track of).