Dice rolling (New to board)

Hello everyone. This is my first post to this board. Forgive me if I mess a few things up or don't know how to do some things. Anyways, on to the problem. My professor wants me to create a program that will roll two dice, add them together, and then tally the sum to show how many times that sum appeared throughout the loop. He wants me to run the program 20 million times. The problem is with my random number equation I think. It doesn't generate a valid dice number (i.e one through six). Can someone tell me what might be wrong? I'll post my code below.

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
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>

using namespace std;

int main()
{
    unsigned seed = time(0);
    int dice1, dice2;
    int total;
    int sum2 = 0, sum3 = 0, sum4 =0, sum5 = 0, sum6 = 0, sum7 = 0, sum8 = 0, sum9 = 0, sum10 = 0, sum11 = 0, sum12 = 0;
    int SIZE = 10;
    int sums[SIZE] = {sum2, sum3, sum4, sum5, sum6, sum7, sum8, sum9, sum10, sum11, sum12};

    srand(seed);

    for (int t = 0; t < 2; t++)
    {
        dice1 = (rand() % 6) + 1;
        dice2 = (rand() % 6) + 1;
        total = dice1 + dice2;
        cout << total;
    if (total = 2)
        sum2++;
    else if (total = 3)
        sum3++;
    else if (total = 4)
        sum4++;
    else if (total = 5)
        sum5++;
    else if (total = 6)
        sum6++;
    else if (total = 7)
        sum7++;
    else if (total = 8)
        sum8++;
    else if (total = 9)
        sum9++;
    else if (total = 10)
        sum10++;
    else if (total = 11)
        sum11++;
    else if (total = 12)
        sum12++;
    }
        cout << "The sum of 2 appeared " << sum2 << " times." << endl;
        cout << "The sum of 3 appeared " << sum3 << " times." << endl;
        cout << "The sum of 4 appeared " << sum4 << " times." << endl;
        cout << "The sum of 5 appeared " << sum5 << " times." << endl;
        cout << "The sum of 6 appeared " << sum6 << " times." << endl;
        cout << "The sum of 7 appeared " << sum7 << " times." << endl;
        cout << "The sum of 8 appeared " << sum8 << " times." << endl;
        cout << "The sum of 9 appeared " << sum9 << " times." << endl;
        cout << "The sum of 10 appeared " << sum10 << " times." << endl;
        cout << "The sum of 11 appeared " << sum11 << " times." << endl;
        cout << "The sum of 12 appeared " << sum12 << " times." << endl;
        
    return 0;
}

(((Forgive me for posting it raw. I don't know how to link it.)))
Last edited on
Welcome, please, use the code tags (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    const int NUM_ROLLS = 20'000'000 ; // 20 million

    // array to hold the frequencies of sums. possible sums are 2, 3, ... 12,
    // so we do not use sum_frequency[0] and sum_frequency[1]
    int sum_frequency[13] = {0} ; // initialise to all zeroes

    // seed the legacy rng, once at the start of the program
    std::srand( std::time(nullptr) ) ;

    for( int i = 0 ; i < NUM_ROLLS ; ++i ) // repeat NUM_ROLLS times
    {
        const int d1 = std::rand()%6 + 1 ; // roll the first die (get a random number in 1...6)
        const int d2 = std::rand()%6 + 1 ; // roll the second die (get a random number in 1...6)

        const int sum = d1 + d2 ; // add the two together
        ++sum_frequency[sum] ; // increment the frequency of this sum
                               // eg. if sum == 4, increment sum_frequency[4]
    }

    // printout the frequencies
    for( int sum = 2 ; sum < 13 ; ++sum ) // for each sum 2 ... 12
        std::cout << "the sum " << sum << " appeared " << sum_frequency[sum] << " times.\n" ;
}

http://coliru.stacked-crooked.com/a/5adf561373d6fe52
You have the classic = bug. comparison is ==. assignment is =. assignment does evaluate to a bool, so it compiles but isn't right... all yours evaluate to true (not zero being assigned into variable).

else if (total == 3)
sum3++;

<ctime> is the modern time.h

The above condense of the code is worth doing, though... all those variables and ifs etc are over-done.
Last edited on
Topic archived. No new replies allowed.