Dice Rolling Program Help

I've been having problems with my program. When I go to compile the program and enter a number to roll the dice the data given is wrong. For instance if I roll the dice once the output data will show a frequency of the total of 11 three times.

Last edited on
(j<=userChoice) j is initialized as 0, so if the user chooses 0 rolls it checks (0<=0) which returns true (because zero is equal to zero). If the user enters 1 then both (0<=1) and (1<=1) will return true. Change the <=
Also, you're not rolling the dice each loop iteration, I'm going to assume that's not your intent?
what im trying to do is find the probabilities of obtaining different results when rolling the dice.
the program is a dice rolling simulator using functions. The object of the program is to ask the user how many times they wish to roll two six sided dice, which will then output how many times a total from 2-12 was rolled and the probability of each total.
the instruction diceTotal = rollDie(6) + rollDie(6); should be inside the while loop, otherwise you are rolling only one time.
while (j<=userChoice) should be while (j<userChoice), otherwise you are rolling userChoice + 1 times.
Finally, the percentage calculations give you wrong results because of the integer divisions. You could do:
1
2
3
4
5
6
7
8
9
10
11
12
13
	double factor = 100.0 / static_cast<double>(userChoice);
	cout << "Total      Frequency         Percentage" << endl;
		cout << "2       " << two   <<"             " << (two * factor) << endl;
		cout << "3       " << three <<"             " << (three * factor) << endl;
		cout << "4       " << four  <<"             " << (four * factor) << endl;
		cout << "5       " << five  <<"             " << (five * factor) << endl;
		cout << "6       " << six   <<"             " << (six * factor) << endl;
		cout << "7       " << seven <<"             " << (seven * factor) << endl;
		cout << "8       " << eight <<"             " << (eight * factor) << endl;
		cout << "9       " << nine  <<"             " << (nine * factor) << endl;
		cout << "10      " << ten   <<"             " << (ten * factor) << endl;
		cout << "11      " << eleven<<"             " << (eleven * factor) << endl;
		cout << "12      "<< twelve<<"              "<<  (twelve * factor) << endl;
You can also make this a lot easier using arrays:
1
2
3
4
5
6
7
8
9
10
int counts[13] = {0};
...
while (j < userChoice) {
    diceTotal = rollDie(6) + rollDie(6);
    ++counts[diceTotal];
}
...
for (int i=2; i<=12; ++i) {
   cout << i << "   " counts[i] << "   " << (double)i / userChoice << endl;
}


You will need to do some formatting on that cout statement, but you get the idea.
Topic archived. No new replies allowed.