gettin the average

Hi, i have a file with these following numbers and i want to take it as an array and take the average, i could write this much but im getting wrong average:
34
99
12
54
79
100
3
13
17
68
93
21
46
42
83



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
  
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
double getAverage(int arr[], int size) 
{
	double avg=0 ;
	for (int i = 0;i < size;i++)
	{
		avg += arr[i];
	}

	avg = avg / size;

	return avg;
}

int main()
{
	// size of array

	int num[100] ;
	double avg = 0;
	int size=0 ;

	ifstream fin;
	fin.open("numbers.txt");

	for (int i = 0; fin.eof() == false; i++) 
	{
		fin >> num[i];
		size = i;
	}
	
	avg = getAverage(num, size);

	cout << avg << endl;

	fin.close();

	

	return 0;
}
Because the size = i; instructions is not executed the last time.
Have a look at the following output:

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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
double getAverage(int arr[], int size) 
{
	double avg=0 ;
	for (int i = 0;i < size;i++)
	{
		avg += arr[i];
	}

	avg = avg / size;

	return avg;
}

int main()
{
	// size of array

	int num[100] ;
	double avg = 0;
	int size=0 ;

	ifstream fin;
	fin.open("numbers.txt");

    int i = 0;
	for (/**/; fin.eof() == false; i++) 
	{
		fin >> num[i];
		size = i;
        cout << "Inside loop - i: " << i << "; size: " << size << '\n';
	}
    cout << "Outside loop - i: " << i << "; size: " << size << '\n';
	
	avg = getAverage(num, size);

	cout << avg << endl;

	fin.close();

	

	return 0;
}


Output:
Inside loop - i: 0; size: 0
Inside loop - i: 1; size: 1
Inside loop - i: 2; size: 2
Inside loop - i: 3; size: 3
Inside loop - i: 4; size: 4
Inside loop - i: 5; size: 5
Inside loop - i: 6; size: 6
Inside loop - i: 7; size: 7
Inside loop - i: 8; size: 8
Inside loop - i: 9; size: 9
Inside loop - i: 10; size: 10
Inside loop - i: 11; size: 11
Inside loop - i: 12; size: 12
Inside loop - i: 13; size: 13
Inside loop - i: 14; size: 14
Outside loop - i: 15; size: 14
48.6429

Thnak youuu soo Muuch, but how can I fix that?
Look at my code: the solution is staring you in the face ;-)
The .eof() is not necessarily accurate way to end the loop.

Consider that each line ends with end-of-line. Even the last one:
42^M

(The ^M represents the end-of-line.)

You read the "42". You are not at the end, for there is still one whitespace character left. You try to read one more number, but there are no numbers after the 42. You fail.

Nevertheless, you think that you have got a value.


1
2
3
4
5
6
7
8
9
10
int value = 0;
size_t count = 0; // unsigned counter to show intent
while ( count < 100 && fin >> value )
{
  // if we get here, then
  // * the value is a valid number
  // * we have got one more value
  // * array still has "free" element to store the value in
}
// assert: count is the number of values AND <= 100 


The fin >> value returns fin. The fin converts to true, if the input operation was successful.
oh i got it i just needed count++ after the loop, thanx guys
Topic archived. No new replies allowed.