Calculate the sum of a text file

Hi, I need help with a program i'm writting, where the user has to enter a file with numbers, and then after I need to work out the sum of the contents, the biggest number, smallest number and average. I'm using textpad 5, with compiler MinGW. I can get my program to show the contents, but the sum is completely wrong.
Any help would be useful
Thank you Liam.

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
// Read the data from the file into an array

	cout << "These are the contents of the file: " << endl;


	while (myfilein.good())

{

    		cout << (char) myfilein.get();

}


	// Calculate and print results on screen.
	/*
		To do: remember to calculate max, min,
		sum and mean using functions
	*/

	cout << endl << endl;

	while (myfilein >> N)

{
        sum = sum + N;
}


	cout << "The sum of the contents the file is: " << sum << endl;
/*	cout << "The maximum value of the file is: " << max << endl;
	cout << "The minimum value of the file is: " << min << endl;
	cout << "The mean value of the file is: " << mean << endl;
*/



	return 0;
}

/*To do - function definitions*/


If you would like to see the rest the program you just have to ask.
Did you forget it initialise sum to zero?
Yes, I did, and the answer is 0. When I don't the answer is like 5e-10. I need to save the file contents (5 numbers) to an array, i think that may be the problem, but i don't know how to save it to an array.
Thanks Liam.
Last edited on
Topic archived. No new replies allowed.