### Five Dice Roll: Vectors

My issue is that my program dies whenever it attempts to readout the average and sum of the rolls. I think I need to include the size function in here somewhere but am not sure how to go about it. Possibly scrap the "do while" loop and go with something else, or just am completely missing something.
Here's the code:

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

int sum5(int d1, int d2, int d3, int d4, int d5);
int DiceRoll();
bool fiveOfAKind(int n1, int n2, int n3, int n4, int n5);

int main()
{
vector<int> DiceRoll;
int i =0, sum=0, counter=0, avg=0;
srand(static_cast<unsigned>(time(NULL)));
int d1=0, d2=0, d3=0, d4=0, d5=0;

cout << "This program will calculate the number of 5 of a kind rolls." << endl;
DiceRoll.size();

do
{
d1=rand()%6+1;
d2=rand()%6+1;
d3=rand()%6+1;
d4=rand()%6+1;
d5=rand()%6+1;
DiceRoll.push_back(sum5(d1, d2, d3, d4, d5));
counter++;
}
while(fiveOfAKind(d1, d2, d3, d4, d5)==false);
for (i=0; i<DiceRoll.size(); i++)
{
sum += DiceRoll[i];
avg=sum/i;
}

cout << "The number of rolls it took to get five of a kind was " << counter << endl;
cout << "The average roll was " << avg << endl;
cout << "The sum of the rolls is " << sum << endl;
return 0;
}

int sum5(int d1, int d2, int d3, int d4, int d5)
{
int result;
result=0;
result=d1+d2+d3+d4+d5;
return result;
}
bool fiveOfAKind(int d1, int d2, int d3, int d4, int d5)
{
if (d1==d2 && d2==d3 && d3==d4 && d4==d5)
{
return true;
}
else
{
return false;
}
}
 ``12345`` ``````for (i=0; i
i is 0 the first time the loop runs and you're dividing by 0. You're lucky the universe isn't gone.
The error (divide by zero) is pretty self explanitory:

 ``12345`` ``````for (i=0; i

The average calculation should go outside the loop anyway. And i is not what you should be dividing by... you should be dividing by the number of elements you summed.
I promise I'm not that stupid, but I guess I am...

Luckily enough part of my program requirements had me implement the average and sum calculations inside the loop and I just never noticed the obvious division by zero, guess I should go home and rethink my life.

Thanks guys.
42
Last edited on
Topic archived. No new replies allowed.