my calculations are off in c++ program. The homework assignment is:
"Write a program that reads all the number in the "Random.txt" file and calculates the following:
a. The number of numbers is the file (the final count) b. The sum of all the numbers in the file (total) c. The average of all the numbers in the file (total/count)
Your program should then display the number of numbers found in the file, the sum of the numbers, and the average of the numbers."
Correct sum is 105,527, I get 106,042 Correct average is 528 (if rounded), I get 530
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("/Users/KJVeliz/Desktop/Random.txt");
double num, totalCount, avg;
totalCount = 0;
int count = -1;
while (!inputFile.eof())
{
inputFile >> num;
count++;
totalCount += num;
avg = totalCount / count;
}
cout << "The number of numbers in the file is: " << count << endl;
cout <<"The sum of the numbers is: " << totalCount << endl;
cout << "The average of the numbers is: " << avg << endl;
return 0;
}
Any help you can offer as to why my calculations are not matching up is much appreciated. I am new to the programming world, so if your answers could be in as much plain English as possible, that would help.