Need help with programming class task!

I just started this university course and there is a programming module which I am terrible at :(

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.

#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
}

I had no idea where to start so I tried writing stuff on paper and I ended up with this code (below). Also because we have only been taught FOR, WHILE and DO WHILE loops I can only use them in this program.

I cant even test this myself properly until much later this week as my mac wont run the same software used at uni, hence why I have no idea at all whether Im on the right tracks and whether what I have done is somewhat right.

#include <iostream>
using namespace std;

#include <ctime> //gets date and time info
int main()
{

int number_of_rolls, iterations, sixes, input;
number_of_rolls = 0;
iterations = 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;

//at this stage we have rolled a 6

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;
}

Last edited on
On this site, use code tags:


[code]

//Your code Here

[/code]



If you don't have a compiler, you can use an online one:

https://www.onlinegdb.com/

or

http://cpp.sh/


MAKE SURE to have your own comfortable coding environment - You NEED it. Use something like Visual Studio, it's free and amazing.


When coding a program, start off by breaking it into parts:

• Program rolls a dice, generates a random number between 1 and 6
• 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
• The program will display the average number of rolls needed to get a six, and the number of sixes that average was based on


Do them ONE at a time. Once you've got one functional, go to the next one. The code provided for one already does the first condition. It creates a random number between 1 and 6, and saves it for you in the variable "roll".

Now, you should use a loop as you have (a for loop is the best here but either one can be used), put that random number generator into a loop and let it loop until "roll" is equal to 6. The issue with your code is that you did this, but you're not checking of "roll" is equal to 6, instead you're MAKING roll equal 6. You're using the "=" operator which assigns, what you want to use is "==", which will CHECK if "roll" and 6 are the same value.
Thanks for the reply mate, still very new to programming and trying to get my head around this. I am getting output on the code but it seems I am getting stuck in the loop for asking if the user wants to continue, no matter what the input is :/



Do you want to continue? Postive number for yes, 0 for no: 1                                                            
Do you want to continue? Postive number for yes, 0 for no: 0                                                            
Do you want to continue? Postive number for yes, 0 for no: 1                                                            
Do you want to continue? Postive number for yes, 0 for no: 0                                                            
Do you want to continue? Postive number for yes, 0 for no:  

etc...



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
48
49

#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;
}
You're making little mistakes !

while (input=!0)

Should Be:

while (input != 0)

You're just starting, but know that you can make this program more efficient (less code). Once everything is working, you should edit it to make it more efficient as practice.
Yeah I don't need the code to be super efficient at this stage, just need to get everything working and have something to show. Cant get help from uni until monday at earliest, thats why Im kinda panicking/desperate to make some progress lol
1
2
3
4
5
    do
    {
        roll = rand() % 6 + 1; //generate a random number between 1 and 6
        number_of_rolls = number_of_rolls + 1;
    }while ((roll ==6));


If you are counting rolls until you hit a 6, this loop is incorrect. This actually counts the number of rolls you make until you don't roll a 6. If roll == 6, the while condition is true, and you loop again.

If you want to break out of the loop after you roll a 6, change the while condition to !=.
As I replied in your other post on the same topic (please don't create multiple posts on the same topic!):

http://www.cplusplus.com/forum/beginner/263081/#msg1135586

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
Topic archived. No new replies allowed.