So, I am currently trying to make a program where it reads a file called "input.txt" with integers stored as follows and calculate how many percentage of them are greater than zero, less than zero, and equal to zero.
10
-4
0
34
42
-2
0
here's my code:
using namespace std;
ifstream inputFile;
int count = 0;
int value,negative,positive,zero;
double negPerc,posPerc,zeroPerc;
inputFile.open("input.txt");
if ( !inputFile.fail()){
while (inputFile >> value)
{
count++;
if(value < 0)
negative++;
if(value == 0)
zero++;
if(value > 0)
positive++;
}
}
else
{
cout << "\nError, unable to open input file ";
}
cout << fixed << setprecision(1);
negPerc = (negative/count)*100;
posPerc = (positive/count)*100;
zeroPerc = (zero/count)*100;
cout << "There were " << negPerc << "% negative numbers." << endl;
cout << "There were " << zeroPerc << "% numbers equal to zero." << endl;
cout << "There were " << posPerc << "% numbers greater than zero." << endl;
and outout:
There were 1864443476.0% negative numbers.
There were 204178000.0% numbers equal to zero.
There were 0.0% numbers greater than zero.
I double checked my code and tried diagnosing why it is this way but I could not find any problems. What am I doing wrong?