2017-03-26 19 views
0

newbie here to C++。 私の教授が私たちに依頼したこのダイスシミュレータの正しいフォーマット+結果をパーセンテージ形式で表示するのに問題があります。私は彼女の指示をここにコピーして貼り付けて、自分のミスを修正する方法を助けてくれてありがとうと思います。ありがとうございました!C++ダイスシミュレータの結果のパーセンテージ出力を表示する方法

INSTRUCTIONS:

このプログラムは、C++乱数関数を使用して単一のダイ(ダイ)(1-6)のロールをシミュレートしなければなりません。最初に、ダイス(ダイス)を何回転がしたいかをユーザーに尋ねます。

次に、ユーザーが要求したダイ(ダイス)のロール数をシミュレートして、ダイ(ダイス)が各ロールごとにどの番号に到達したかを記録します。プログラムの終わりに、ダイ(ダイス)ロールが各番号に何回着いたか、ダイ(ダイス)ロールが各番号に何回着いたかを示すレポートを印刷します。

講義中に私があなたに示したことを使用して、関数や配列を使用しないでください。講義中に私が言ったことを忘れてしまったら、スライドを見ている間、

入力検証:サイコロを振りたい回数として1未満の数字を入力できないようにします。

出力は(右側)何鉱山の出力対(左に)する必要があります方法:

enter image description here

#include <iostream> 
    #include <iomanip> 
    #include <string> 
    #include <cstdlib> 
    #include <ctime> 
    using namespace std; 

    int main() 
    { 
    srand((unsigned int)time(NULL)); 

    int timesRolled; 
    int rolled; 
    float num1 = 0; 
    float num2 = 0; 
    float num3 = 0; 
    float num4 = 0; 
    float num5 = 0; 
    float num6 = 0; 

cout << "How many times would you like to roll the dice?" << endl; 
cin >> timesRolled; 

for (int i = 0; i < timesRolled; i++) 
    { 
     rolled = rand() % 6 + 1; 

     if (rolled == 1) 
     { 
      num1++; 
     } 

     else if (rolled == 2) 
     { 
      num2++; 
     } 

     else if (rolled == 3) 
     { 
      num3++; 
     } 

     else if (rolled == 4) 
     { 
      num4++; 
     } 

     else if (rolled == 5) 
     { 
      num5++; 
     } 

     else if (rolled == 6) 
     { 
      num6++; 
     } 
    } 

while (timesRolled < 1) 
    { 
     cout << "This is an invalid number. " << endl 
      << "The number of rolls should be equal to or greater than 1." << endl 
      << "Please enter again." << endl; 
     cin >> timesRolled; 
    } 

    cout << "\nDICE ROLL STATISTICS" << endl << endl 
    << "# Rolled  # Times  % Times" << endl 
    << "--------  -------- --------" << endl 
    << setw(7) << "1" << setw(17) << fixed << setprecision(0) << num1 << setw(17) << fixed << setprecision(2) << (num1/timesRolled) * 100 << "%" << endl 
    << setw(7) << "2" << setw(17) << fixed << setprecision(0) << num2 << setw(17) << fixed << setprecision(2) << (num2/timesRolled) * 100 << "%" << endl 
    << setw(7) << "3" << setw(17) << fixed << setprecision(0) << num3 << setw(17) << fixed << setprecision(2) << (num3/timesRolled) * 100 << "%" << endl 
    << setw(7) << "4" << setw(17) << fixed << setprecision(0) << num4 << setw(17) << fixed << setprecision(2) << (num4/timesRolled) * 100 << "%" << endl 
    << setw(7) << "5" << setw(17) << fixed << setprecision(0) << num5 << setw(17) << fixed << setprecision(2) << (num5/timesRolled) * 100 << "%" << endl 
    << setw(7) << "6" << setw(17) << fixed << setprecision(0) << num6 << setw(17) << fixed << setprecision(2) << (num6/timesRolled) * 100 << "%" << endl; 

system("PAUSE"); 
return 0; 
} 
私は(私のプログラムの最後で)てSetPrecisionを使用しようとしたところあなたが見ることができます

最終的なパーセンテージで小数点以下の桁数を操作することはできますが、動作していないように見えます。

+0

[整数の除算の可能性のある重複常にゼロ](http://stackoverflow.com/questions/9455271/integer-division-always-zero) – user4581301

答えて

2

あなたの問題は、例えば、整数の除算である:

(num1/timesRolled) * 100 = 10/50 * 100 = 0 * 100 = 0; 

使用浮動小数点値の代わりにまたは分割前のキャスト:

float num1 = 0; 

または

(static_cast<float>(num1)/timesRolled) * 100 
+1

Downvote 。宿題に関する微妙なヒントが役立ちます。彼らのために宿題をすることは賢明ではありません。 – zipzit

+0

だから、整数の代わりに整数num1、num2、num6をfloatに変更し、今度は0.00を得るようになりましたが(小数はパーセント出力のため正しく機能しています)、#timesの出力にも影響しているようです10進形式ではないはずです編集:私はそれを解決したと思います。調整を表すために上のコードを更新しました。 – Khovsepa

関連する問題