So I'm writing a program to roll 2 dice and record how many times a specific sum comes up, show the odds and the percentage error of what the rolls provide. However anytime the odds and the actual number of the specific sum is rolled the percentage error (which should be zero), comes up as a crazy number. I believe this is because it results in (0/1) in the cout statement, but (0/1) equals zero in C++ as I have tested it. Source code is below, it is not complete and the only sum being tested is a sum of "2", i.e. both dice roll a 1. Output of code with problem
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string>
#pragma warning(disable: 4996)
using namespace std;
int main() {
int dice1;
int dice2;
int numRolls = 36;
char again = 'Y';
int sum;
int numRolled[11] {0,0,0,0,0,0,0,0,0,0,0};
srand(time(NULL));
while (again == 'Y' || again == 'y') {
cout << "Please enter the number of rolls you want :";
cin >> numRolls;
for (int i=0; i<numRolls; i++) {
dice1 = 1 + rand()%6;
dice2 = 1 + rand()%6;
//cout << dice1 << " " << dice2 << endl;
sum = dice1 + dice2;
switch (sum) {
case 2 :
numRolled[0]++;
break;
case 3 :
numRolled[1]++;
break;
case 4 :
numRolled[2]++;
break;
case 5 :
numRolled[3]++;
break;
case 6 :
numRolled[4]++;
break;
case 7 :
numRolled[5]++;
break;
case 8 :
numRolled[6]++;
break;
case 9 :
numRolled[7]++;
break;
case 10 :
numRolled[8]++;
break;
case 11 :
numRolled[9]++;
break;
case 12 :
numRolled[10]++;
break;
default :
cout << "There was a problem" << endl;
}
}
cout << 0.00/1.00 << endl; //This was the test of 0/1 to see what it returned
cout << "Sum " << " #Rolled" << " Odds " << " Percent Error " << endl;
cout << " 2 " << numRolled[0] << " " << (1.00 / 36.00) * numRolls << " " << ((numRolled[0])-(1.00/36.00)*numRolls)/((1.00/36.00)*numRolls) * 100 << endl;//This is the line that I believe is causing a problem.
for (int j=0; j<11; j++) {
numRolled[j] = 0;
}
cout << "Would you like to roll again? (Y)/(N) :";
cin >> again;
}
system("pause");
return 0;
}