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.
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
1 2
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"
#include <cstdlib>
#include <iostream>
#include <ctime>
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";
}