Mar 21, 2017 at 10:52pm UTC
you need to store value of your
lastRoll
and compare it to your
currentRoll
. So you while loop should be
while (lastRoll != currentRoll)
Also a
do while
loop does not seem to work well here. just using a
while
loop is easier. Let me know if you'd like me to explain the code further.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int dice1, dice2, lastRoll = 666, counter = 0, totalRoll = 333;
srand(time(NULL));
while (lastRoll!=totalRoll)
{ dice1 = rand()%6+1;
dice2 = rand()%6+1;
totalRoll = dice1 + dice2;
cout << dice1 << "+" << dice2 << " = " << totalRoll <<endl;
counter++;
if (lastRoll!=totalRoll)
{
lastRoll = totalRoll;
totalRoll = 333;
}
}
cout << "It took: " << counter << " tries" ;
return 0;
}
Last edited on Mar 21, 2017 at 11:05pm UTC
Mar 21, 2017 at 11:07pm UTC
Thank you for helping me, the one thing I don't understand is why the totalRoll and lastRoll is equal to 333 and 666?
Last edited on Mar 21, 2017 at 11:08pm UTC
Mar 21, 2017 at 11:10pm UTC
You're Welcome! And totalRoll = 333
just to initialize it into a value i can use to identify errors. It could be 0 as long as it does not equal the initialized value of lastRoll
or else the while
loop wont execute.
Ohhh totalRoll
in line 24 is set to 333 because if i didn't change the value totalRoll
would equal lastRoll
making the while statement true even though the dice were not rolled twice in a row.
Last edited on Mar 21, 2017 at 11:13pm UTC
Mar 21, 2017 at 11:13pm UTC
I see, thank you very much
Mar 21, 2017 at 11:19pm UTC
Hope i explained it well enough. I should have used a bool for the while statement to make it easier to read! Lol my apologies.