0

newbie here to C++. I am having trouble displaying the correct format + results in percentage form for this dice simulator our professor has asked us to make. I'll copy/paste her instructions here and would appreciate help with how to correct my mistake(s) in regard to the formatting of the percentage output in my program. Thank you!

INSTRUCTIONS:

This program should simulate the roll of a single die (dice) (1-6) using the C++ random number functions. First ask the user how many times they would like to have the die (dice) rolled.

Next, have the program simulate the number of rolls of the die (dice) the user requested and keep track of which number the die (dice) landed on for each roll. At the end of the program print out a report showing how many times the die (dice) roll landed on each number and what percentage of the total times the die (dice) roll landed on each number.

Do NOT use functions or arrays on this - use what I showed you during lecture, you should always listen during lecture to get the right techniques, if you forgot what I said during lecture look at the slides.

Input Validation: Do not allow the user to enter a number less than 1 as the number of times they would like to roll the dice.

How output should be (on left) vs what mine outputs (on right):

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

You can see where I tried to use setprecision (at the end of my program) to manipulate the output of decimals in my final percentage number, but it doesn't seem to be working and this is where I need help please.

Khovsepa
  • 37
  • 7
  • Possible duplicate of [Integer division always zero](http://stackoverflow.com/questions/9455271/integer-division-always-zero) – user4581301 Mar 26 '17 at 08:45

1 Answers1

2

Your problem is an integer division e.g.:

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

Use floating point values instead or cast before division:

 float num1 = 0;

or

(static_cast<float>(num1) / timesRolled) * 100
Alex
  • 9,891
  • 11
  • 53
  • 87
  • So I changed all the int num1, num2, through num6 to float instead of int and now I get 0.00 (as in the decimals are working properly for percentage output), but it appears that it's also affecting the output of #times which is NOT supposed to be in decimal format EDIT: I believe I have solved it. Updated my code above to represent adjustments. – Khovsepa Mar 26 '17 at 08:38