2016-10-26 4 views
1

So versuche ich derzeit ein Programm zu machen, wo es liest eine Datei namens "input.txt" mit Ganzzahlen wie folgt gespeichert und berechnen, wie viel Prozent von ihnen größer als Null sind, weniger als Null und gleich Null.Int lesen pro Zeile und Prozentsatz anzeigen

10 
-4 
0 
34 
42 
-2 
0 

hier ist mein 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; 

und outout:

There were 1864443476.0% negative numbers. 
There were 204178000.0% numbers equal to zero. 
There were 0.0% numbers greater than zero. 

Ich doppelt überprüft meinen Code und versucht, die Diagnose, warum es so ist, aber ich konnte keine Probleme finden. Was mache ich falsch?

+2

Die Divisionen sind 'int/int', was zu einem' int' abschneidet. Wirf einen von ihnen zu einem 'doppelten' * vor * der Division. Zum Beispiel könnten Sie es als '(negative * 100.0)/count;' schreiben. Das ist nur ein Fehler. – BoBTFish

+0

Dies sind beide Ganzzahlen - negativ und zählen - so erhalten Sie Integer-Division, die nicht das ist, was Sie erwarten. – UKMonkey

+5

Initialisiere auch negative, positive und null auf "0" (wie yo tat mit count) vor dem Inkrementieren. Kompilieren Sie mit -g -Wall und führen Sie alles durch, was der Compiler beschwert. Schauen Sie auch in "Namespace-Verschmutzung" (beenden Sie die Verwendung von "using namespace std"). – Chris

Antwort

0

Hier ist Ihr Code mit allen Kommentaren zusammengestellt für Sie zu studieren.

// 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; 
}