1

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?

user3521038
  • 21
  • 1
  • 3
  • 2
    The divisions are `int / int`, which will truncate to an `int`. Cast one of them to a `double` *before* the division. For instance, you could write it as `(negative*100.0)/count;`. That is just one error. – BoBTFish Oct 26 '16 at 15:56
  • These are both integers - negative & count - so you get integer division, which is not what you're expecting. – UKMonkey Oct 26 '16 at 15:56
  • 5
    Also, initialize negative, positive and zero to "0" (like yo did with count) prior to incrementing them. Compile with -g -Wall and run down everything the compiler is complaining about. Also, look into "namespace pollution" (stop using "using namespace std"). – RigidBody Oct 26 '16 at 15:58
  • 1
    Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Oct 26 '16 at 16:05
  • 1
    I am using `namespace std` and `endl` because that is how my professor told me to do so far ( I am attending intro c++ class at the moment). I would love to learn it the other way when time comes. – user3521038 Oct 26 '16 at 16:15
  • @BoBTFish Thank you. I fixed the percentage to 100.0 – user3521038 Oct 26 '16 at 16:16
  • 1
    @Chris I initialized like what you told me but it is still giving me the same value. – user3521038 Oct 26 '16 at 16:17
  • See answer - study it, chock full o good stuff. Good luck! – RigidBody Oct 26 '16 at 17:06

1 Answers1

0

Here is your code with everyone's comments put together for you to study.

// Compile with "g++ yourCPPfile.cpp -o yourExecName -g -Wall"
#include <iostream>
#include <iomanip>
#include <fstream>
using std::ifstream;
#include <cstdio>
using std::cout;

int main() {
    ifstream inputFile;
    int count, value, negative, positive, zero;
    count = negative = positive = zero = 0;
    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 " << inputFile << "!\n";
    }

    // Stays this way until you setPrecision() to something else.
    cout << std::setprecision(1);

    // Troubleshooting!!
    cout << negative << "\n";
    cout << positive << "\n";
    cout << zero << "\n";
    cout << count << "\n";

    // Calculations.
    negPerc = (negative * 100.0 / count);
    posPerc = (positive * 100.0 / count);
    zeroPerc = (zero * 100.0 / count);

    // Your desired result...
    cout << "There were " << std::fixed << negPerc << "% negative numbers." << "\n";
    cout << "There were " << std::fixed << zeroPerc << "% numbers equal to zero." << "\n";
    cout << "There were " << std::fixed << posPerc << "% numbers greater than zero." << "\n";
    return 0;
}
RigidBody
  • 656
  • 3
  • 11
  • 26