#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream, inputfile, inFile;
int num = 0, sum = 0, count = 0;
double average;
//Open the file.
inputFile.open("numbers.txt");
cout << "Reading data from the file.\n";
//If the file successfully opened, process it.
if (inputFile)
{
//Count the total number of
//numbers in the file.
while (inputFile >> num)
{
count++;
sum += num;
}
//Close the file.
inputFile.close();
}
else
{
//Display an error message.
cout << "Error opening the file.\n";
}
average = sum / count;
cout << "There are " << num << " numbers in the file. \n";
cout << "The sum of the numbers is " << sum << ".\n";
cout << "Lastly the average is " << average << endl;
return 0;
}
average = sum / count;
You will need to cast either operands to a double, as integer division will truncate the fractional part. Other than that, I don't see anything that could cause any errors, assuming numbers.txt is formatted correctly.