Classifying numbers from file - All registered as zero?

Hey guys, I'm trying to make a program that reads a file (which has a list of integers) , checks each integer if it is a zero, odd, or even and then increase the count of zeros, odd and evens based on that list. Then I want to calculate the sum and average of that list, then print out the results on another file. My code is as follows:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

void classify(int n, int& z, int& o, int& e)
{
	if (n == 0)
	{
		z++;
	}
	else
	{
		switch (n % 2)
		{
		case 0:
			e++;
			break;

		case 1:
		case -1:
			o++;
			break;
		}
	}
}

 void counting(std::ifstream& file, int n, int& countr)
{
	file >> n;
	if (n > 0 || n < 0)
	{
		countr++;
	}
}
int main()
{
	int zero = 0, odd = 0, even = 0, count = 0, sum = 0;
	int num = 0;
	double average;

	std::ifstream ifile;
	std::ofstream ofile;

	ifile.open("input.txt");
	ofile.open("output.txt");
	
	
	while (!ifile.eof())
	{
		counting(ifile, num, count);
	}

	std::cout << count; //shows that there are 10 numbers in the file
	

	int arr[100];
	
	
	for (int i = 0; i < count; i++)
	{

		ifile >> num;
		std::cout << num;
		classify(num, zero, odd, even);
		arr[i] = num;
	}


	for (int i = 0; i < count; i++)
	{
		sum = sum + arr[i];
	}

	average = static_cast<double>(sum / count);

	ofile << "Number of zeros: " << zero << "\t odd: " << odd << "\teven: " << even << "\nSum = " << sum << "\nAverage = " << average;

	ifile.close();
	ofile.close();


The input file contains the following:
1 2 3 4 5 6 7 -8 9 10

and the output file produces:
Number of zeros: 10 odd: 0 even: 0
Sum = 0
Average = 0

So for some reason my program is considering each integer to be a zero? Hence the reason why sum and average = 0.

Can anyone explain why this is happening? I've made a similar program except the list was from user input, not a file and it worked fine.

Thanks for any assistance :)
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
#include <iostream>
#include <fstream>

void update_counts( int number, int& cnt_zero, int& cnt_odd, int& cnt_even )
{
    if( number%2 != 0 ) ++cnt_odd ;
    else number == 0 ? ++cnt_zero : ++cnt_even ;
}

int main()
{
    const char* const in_file_name = "input.txt" ;
    const char* const out_file_name = "output.txt" ;

    { std::ofstream(in_file_name) << "0 1 2 -3 -4 5 6 7 -8 9 10 0\n" ; } // create input file

    std::ifstream infile(in_file_name) ;
    int number, cnt_zero = 0, cnt_odd = 0, cnt_even = 0, sum = 0 ;
    while( infile >> number )
    {
        sum += number ;
        update_counts( number, cnt_zero, cnt_odd, cnt_even ) ;
    }

    const int cnt = cnt_zero + cnt_odd + cnt_even ;
    if( cnt != 0 )
    {
        const double average = double(sum) / cnt ;
        std::ofstream(out_file_name) << "Number of zeros: " << cnt_zero << "    odd: "
                                     << cnt_odd << "    even: " << cnt_even
                                     << "\nSum = " << sum
                                     << "\nAverage = " << sum << '/' << cnt << " = "
                                     << average << '\n' ;
    }

    // for verification: dump contents of output file
    { std::cout << std::ifstream(out_file_name).rdbuf() ; }
}

http://coliru.stacked-crooked.com/a/8cab9a689fe9f197
Topic archived. No new replies allowed.