Help w/ avg, sum, num from file.

Struggling to get the program to work correctly. I'm needing to read a file and count the numbers, find the average, and figure the sum.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>

using namespace 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;
}
Last edited on
Well my sum and average is working but having issues with the count. How do I make it stop at the end of the list?
Last edited on
Topic archived. No new replies allowed.