2016-10-26 3 views
1

したがって、私は現在、以下のように整数を格納した "input.txt"というファイルを読み込み、そのうちの何パーセントが0より大きく、より小さいかを計算するプログラムを作成しようとしていますゼロ、ゼロに等しい。intを1行で表示し、パーセンテージを表示する

10 
-4 
0 
34 
42 
-2 
0 

は、ここに私のコードです: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; 

とoutout:

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

私は二重の私のコードをチェックし、それがこの方法ですが、私はすべての問題を見つけることができなかった理由を診断してみました。私は間違って何をしていますか?

+2

分割は 'int/int'で、' int'に切り詰めます。それらの1つを部門の前に「ダブル」*にキャストします。例えば、 '(negative * 100.0)/ count;'と書くことができます。それはただ1つのエラーです。 – BoBTFish

+0

これらは両方とも整数です - 負数とカウント - 整数分裂を得ます。これはあなたが期待していないものです。 – UKMonkey

+5

また、インクリメントする前に負、正、ゼロを "0"に初期化します(countで指定したように)。 -g -Wallを指定してコンパイルし、コンパイラが不平を言っているすべてを実行します。また、 "namespace std"を使用して "名前空間汚染"を調べてください。 – Chris

答えて

0

ここにあなたの勉強のために皆のコメントをまとめたコードがあります。

// 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; 
} 
関連する問題