### Can someone help me write this program?

closed account (4wRjE3v7)
This is my code so far, but I know it's not correct, can someone give me helpful tips thank you!P.S I have to use a for loop.
-Michelle

I have to write a program that roles a single die using the random generator until a five is rolled. The program should count the number of rolls that were taken before the five was rolled an output this number at the end.
 ``123456789101112131415161718192021222324252627`` ``````#include #include using namespace std; int main() { srand((unsigned)time(NULL)); int i=0,secret,num,roles=0; secret = rand() % 6 + 1; for(i; i<20; i++) {cout << "Welcome to the role the dice game\n"; cout << "Please press any number to start the role "; cin >> num; if(secret!=5) { roles++; } if(num==secret && secret==5) { cout<<"The dice rolled" << roles << "times before rolling 5."; return 0; } } }``````

Last edited on
There are a couple of issues here. First off, why do you loop to 20 here `for(i; i<20; i++)`? I would take everything out of that for-loop, and instead do a while-loop after
 ``12`` ``````cout << "Please press any number to start the role "; cin >> num;``````
, which would look something like `while(secret != 5)`. Inside that loop I would then do `secret = (rand()%6) + 1;` to actually "roll the dice"
closed account (4wRjE3v7)
@fafner but the assignment does not allow me to use a while loop
 ``12345678910111213141516171819202122232425262728`` ``````#include #include #include int main() { srand((unsigned)time(NULL)); int i=0,secret,num,roles=0; std::cout << "Welcome to the role the dice game\n"; for(i; i<20; i++) { std::cout << "Please press any number to start the role "; std::cin >> num; secret = rand() % 6 + 1; if(secret!=5) { roles++; } else { std::cout<<"The dice rolled" << roles << "times before rolling 5."; return 0; } } std::cout << "The dice did not roll 5 once.\n"; }``````