dice roll project

Iv been trying to get this program for my university course to work but I am completely stuck and have no idea what Im doing really. I only started programming last week and I am struggling to complete this task.

Write a new .cpp file, called Tut3_7 that rolls a dice (i.e. generates a random number between 1 and 6) and counts how many times the dice is rolled until it gets a 6. The program will then ask the user if they want to continue – by entering a positive number – or terminate – by entering a ‘0’. The program will display the average number of rolls needed to get a six, and the number of sixes that average was based on. Hint: the following code will help you generate a random number. (difficulty: 3)


#include <cstdlib> // random numbers header file//

#include <ctime> // used to get date and time information

int main()

{

srand(time(0)); //initialise random num generator using time

int roll = 0; //declare a variable to keep store the random number

roll= rand() % 6 + 1; // generate a random number between 1 and 6

}

So far I have made the following attempt.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
using namespace std;

#include <ctime> //gets date and time info
int main()
{
    
    int number_of_rolls, sixes, input;
    number_of_rolls = 0;
    sixes = 0;
    input = 0;
    
    float average_rolls;
    average_rolls = 0;
    
    srand(time(0)); //initialises num generator using time
    int roll = 0; //declare a variable to store random number
    
    do
    {
        roll = rand() % 6 + 1; //generate a random number between 1 and 6
        number_of_rolls = number_of_rolls + 1;
    }while ((roll ==6));
    
    number_of_rolls = number_of_rolls + 1;
    sixes = sixes + 1;
    
    while (input != 0)
    {
       cout << "Do you want to continue? Postive number for yes, 0 for no: ";
       cin >> input;
        do
        {
           roll = rand() % 6 + 1; //generate a random number between 1 and 6
           number_of_rolls = number_of_rolls + 1;
          }while ((roll ==6));
        
        number_of_rolls = number_of_rolls + 1;
        sixes = sixes + 1;
        
    }
    
    average_rolls = number_of_rolls / sixes;
    cout << "The average number of rolls to get a 6 in this case is: " << average_rolls << " this is based on getting a six " << sixes << " times";
    
    return 0;
}


I am fully lost on how to get this program to work and have no idea where to even start.
line 23 is probably supposed to be !=

which is repeated on 36

you look like you have the idea.
you need to count how many rolls to get a 6. the do loop, once fixed, will do that.
then its just an average: sum of the # of rolls for all runs / number of runs.
so every time you get a 6 you counted that... and number of rolls can just keep going each run getting bigger... so its going to be number of rolls/ sixes is the average. Which should be about 4, since you can't have a fraction of a roll, and its likely to be 3.xxx

most compilers used a poor quality rand(). <random>, look in the documents on this site on how to use it, does better. So with rand you may see strange things.
Last edited on
Try changing while(roll == 6) to while(roll != 6)
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
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>  // srand()/rand()
#include <ctime>    // time()

int main()
{
   // seed the random number generator
   // do it once, do it before generating a random number
   srand((unsigned) time(0));

   double total_number_of_rolls { };
   double total_number_of_sixes { };
   int again                    { };

   while (true)
   {
      int number_of_rolls { };
      int roll            { };

      do
      {
         roll = rand() % 6 + 1;

         std::cout << roll << ' ';

         number_of_rolls++;
      }
      while (roll != 6);
      std::cout << "\t(number of rolls): " << number_of_rolls << "\n\n";

      total_number_of_rolls += number_of_rolls;

      total_number_of_sixes += 1;

      std::cout << "Do you want to go again? (Enter a number, 0 to quit): ";
      std::cin >> again;
      std::cout << '\n';

      if (again == 0) { break; }
   }

   std::cout << "Total number of sixes: " << total_number_of_sixes << '\n';
   std::cout << "Total number of rolls: " << total_number_of_rolls << '\n';
   std::cout << "Average number of rolls: " << total_number_of_rolls / total_number_of_sixes << '\n';
}

4 6     (number of rolls): 2

Do you want to go again? (Enter a number, 0 to quit): 1

4 6     (number of rolls): 2

Do you want to go again? (Enter a number, 0 to quit): 1

2 6     (number of rolls): 2

Do you want to go again? (Enter a number, 0 to quit): 1

5 1 2 1 1 4 5 5 5 1 2 5 3 4 5 5 3 2 6   (number of rolls): 19

Do you want to go again? (Enter a number, 0 to quit): 0

Total number of sixes: 4
Total number of rolls: 25
Average number of rolls: 6.25
Sorry just didn't see the beginners forum when I made the post the other night. Thanks so much for the help man, getting so close now. I am currently looking to complete the task without using an IF statement and by using WHILE(input!=0) or something along those lines as the task was given based on the few things I have been taught so far.
Topic archived. No new replies allowed.