Switch statement into an array.

Hello, I'm trying to store the count of each roll into an array to simplify the code. However, I'm fairly new to C++ programming and could use some help. I have commented out some ideas I have that just don't work. All input is greatly appreciated thank you!

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//Course:

#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
using namespace std;


const int FACES    = 6;    //faces of a standard die
const int LOW_FACE = 1;    //lowest face on a standard die

//function header
int roll2Dice(void);
int roll(void);

int main ()
{
    const int NUM_ROLLS = 120000;    //number of rolls

    //variable declaration
    int num2  = 0;   //number of faces with 2
    int num3  = 0;   //number of faces with 3
    int num4  = 0;   //number of faces with 4
    int num5  = 0;   //number of faces with 5
    int num6  = 0;   //number of faces with 6
    int num7  = 0;   //number of faces with 7
    int num8  = 0;   //number of faces with 8
    int num9  = 0;   //number of faces with 9
    int num10 = 0;   //number of faces with 10
    int num11 = 0;   //number of faces with 11
    int num12 = 0;   //number of faces with 12
    int toss  = 0;   //roll of 2 dice
    int seed;        //seed for random number generator


    int diceArray [13];
    int x = 0;

    cout << "Enter seed: ";
    cin >> seed;
    srand( seed );

    //roll the die NUM_ROLLS times and count number of each roll
    for (int cnt=1; cnt <= NUM_ROLLS; cnt++)
    {
        //roll the dice
        toss = roll2Dice( );

        //sum the number of each roll
        switch (toss)
        {
            case 2:
                num2++;
                break;
            case 3:
                num3++;
                break;
            case 4:
                num4++;
                break;
            case 5:
                num5++;
                break;
            case 6:
                num6++;
                break;
            case 7:
                num7++;
                break;
            case 8:
                num8++;
                break;
            case 9:
                num9++;
                break;
            case 10:
                num10++;
                break;
            case 11:
                num11++;
                break;
            case 12:
                num12++;
                break;
            default:    //should not occur - but let us know
                cout << "Invalid roll of " << toss << endl;
                break;
        }


/*
        while (x <=NUM_ROLLS )
        {
        toss = diceArray[1];
        x++;
        cout << "This is toss " << toss;

        }



        }
/*
//print out the number of each roll
    cout << fixed << setprecision(4);
    cout << endl << "Number of rolls is " << NUM_ROLLS << " rolls" << endl;
    cout << endl;
    cout << "Toss   Rolls    Percent" << endl;
    cout << "-----------------------" << endl;
    cout << 2  << "\t" << num2  << "\t" << (double) num2  / NUM_ROLLS * 100 << endl;
    cout << 3  << "\t" << num3  << "\t" << (double) num3  / NUM_ROLLS * 100 << endl;
    cout << 4  << "\t" << num4  << "\t" << (double) num4  / NUM_ROLLS * 100 << endl;
    cout << 5  << "\t" << num5  << "\t" << (double) num5  / NUM_ROLLS * 100 << endl;
    cout << 6  << "\t" << num6  << "\t" << (double) num6  / NUM_ROLLS * 100 << endl;
    cout << 7  << "\t" << num7  << "\t" << (double) num7  / NUM_ROLLS * 100 << endl;
    cout << 8  << "\t" << num8  << "\t" << (double) num8  / NUM_ROLLS * 100 << endl;
    cout << 9  << "\t" << num9  << "\t" << (double) num9  / NUM_ROLLS * 100 << endl;
    cout << 10 << "\t" << num10 << "\t" << (double) num10 / NUM_ROLLS * 100 << endl;
    cout << 11 << "\t" << num11 << "\t" << (double) num11 / NUM_ROLLS * 100 << endl;
    cout << 12 << "\t" << num12 << "\t" << (double) num12 / NUM_ROLLS * 100 << endl;



*/
    return 0;

    }


 //end main



//--------------------------------
//Function: roll2Dice
//Purpose: Simulate the roll of two dice
//Parameters: none
//Return: sum of two dice rolls
//--------------------------------
int roll2Dice(void)
{
    return (roll() + roll());
}


//--------------------------------
//Function: roll
//Purpose: Simulate the roll of a die
//Parameters: none
//Return: face value of one die
//--------------------------------
int roll(void)
{
    return (LOW_FACE + rand( ) % FACES);
}

















Do it so,

1
2
3
4
5
6
7
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

        switch (toss)
        {
            case 2:
                num[2]++;
                break;


That should work.
Last edited on
Do I encase every num in the array?

Actually I just realized I'm supposed to store each roll into an array. I'm not sure how they are suppose to connect in the code, and I'm very confused by this idea. I understand all the fundamentals just not how they mesh.
Ex:


1
2
3
4
5
6
7
8
9
10
11
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

        switch (toss)
        {
            case 2:
                num[2]++;
                break;
           case 3:
                num[3]++;
                break;
Last edited on
use this for variable declaration. SO much easier it gives an array of 6 characters and setts them all to 0.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void setting(int faces[], int &toss, int &seed)
{
	int i;
	for(i=0; i<highest; i++)
	{
		faces[i]=0;
	}
	toss=0;
	seed=0;
}

void main()
{
int faces[highest];
int toss, seed, i;

//sets All faces to zero
setting(faces, toss, seed);



system("pause");
}
Topic archived. No new replies allowed.