Reading from a file

Problem: Write a program that reads a file containing an unknown number of integers and displays the
percentage of the integers in the file that are zero, the percentage that are negative and the percentage
that are greater than zero.

This code isn't entirely finished. I'm just doing some verification. But for some reason, my counters aren't working properly and I'm not exactly sure why. The input file currently contains:

12
22
-8
0
-5
-6
4
0

But I'm only getting a total number of 4, only 1 positive, 1 negative, and 2 zeroes in my output file. What can I do in order to fix that?

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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inputFile;
	ofstream outputFile;
	int number, positive = 0, negative = 0, zeroes = 0, total;
	float positivepercentage = 0, negativepercentage = 0, zeropercentage = 0;

	inputFile.open("numbers.txt");
	if ( !inputFile.fail())
	{
		cout << "Input file is open.\n";
		outputFile.open("Percentage.txt");
		if ( !outputFile.fail())
		{
			cout << "Output file is open.\n";
			while (inputFile >> number)
			{
				inputFile >> number;
				if (number > 0)
				{
					positive += 1;
				}
				else if (number < 0)
				{
					negative++;
				}
				else 
				{
					zeroes++;
				}
			}
			total = positive + negative + zeroes;
			positivepercentage = positive/total;
			negativepercentage = negative/total;
			zeropercentage = zeroes/total;
		
			outputFile << total << endl;
			outputFile << "the number of positives is: " << positive << endl;
			outputFile << negative << endl;
			outputFile << zeroes << endl;

			outputFile.close();
			inputFile.close();
		}
		else 
		{ 
			cout << "output";
		}
	}
	else
	{
		cout << "input";
	}
	return 0;

}

Topic archived. No new replies allowed.