How do I create a die rolling counter using classes?

I'm trying to create a program that rolls a die a few thousand times and print out how many times it lands on each face and then print out a graph using x's as a visual representation using classes.

output looks like this:
1 --- 2000 xxxxxxxxxxxxxxxxxxxxxxxxx
2 --- 1950xxxxxxxxxxxxxxxxxxxxxxxx
.
.
6 --- 2050 xxxxxxxxxxxxxxxxxxxxxxxxxxx

I need a method called int count(int face) inside my class that returns the number of times a particular face has appeared, I just don't know how to incorporate this. I think I should use a constructor and destructor but i'm new with those and knowing when to actually use them.

Here is what I have so far that shows how it should look when printed, just struggling to convert using a class.

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

class aDie {
public:
    int roll();
    int count(int face);
private:
    int numRolled;
    int faceRolled;
};

int aDie::roll() {
    numRolled = ((rand() % 6) + 1);
    return numRolled;
}

int aDie::count(int face) {
    ???
}

int main() {
    srand(time(NULL));
    int numRolls = 12000;
    int dieSides = 6;
    vector<int> face(dieSides);
    vector<int> copyFace(dieSides);
    
    aDie getRan; //creates object getRan
    for (int i = 0; i < numRolls; ++i) { //gets 12000 rolls
        ++face.at(getRan.roll() - 1); // increments element in vector that matches roll
    }

for (int i = 0; i < dieSides; ++i) {
    copyFace.at(i) = face.at(i); //copies each element of vector face into copyFace so that it can be
}								 //manipulated without affecting the original vector
for (int i = 0; i < (dieSides - 1); ++i) {
    if (face.at(i) > copyFace.at(i + 1)) { // compares the first element with the next to see which is bigger
        int temp = copyFace.at(i); //if it is bigger, swap elements
        copyFace.at(i) = copyFace.at(i + 1);
        copyFace.at(i + 1) = temp;
    }
}

int average = 0;
average = copyFace.at(5) / 60; //takes highest face rolled and finds ratio relative to 60

for (int i = 0; i < dieSides; ++i) {
    cout << (i + 1) << " --- " << face.at(i) << " ";
    face.at(i) = face.at(i) / average; //converts each element to its conversion ratio so that the highest number is 60
    for (int j = 0; j <= face.at(i); ++j) {
        cout << "x"; //prints and x each for every "average" amount of stars so that highest count = 60 x's
        if (j == face.at(i)) { //after last x is printed, ends line for next element in vector face
            cout << endl;
        }
    }
}
    
    return 0;
}


Thanks.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

class die {

    public:
        static const int NUM_FACES = 6  ;

        int roll()
        {
            const int n = std::rand() % NUM_FACES + 1 ;
            ++faceRolled[n] ;
            return n ;
        }

        int count( int face ) const // **** const ****
        { return face > 0 && face <= NUM_FACES ?  faceRolled[face] : 0 ; }

    private:
        int faceRolled[NUM_FACES+1] = {0}; // initialise with all zeroes
};

int main()
{
    const int N = die::NUM_FACES * 32000000 ;
    std::srand( std::time(0) ) ;

    die a_die ;
    for( int i = 0 ; i < N ; ++i ) a_die.roll() ;
    for( int face = 1 ; face <= die::NUM_FACES ; ++face )
        std::cout  << face << ". " << a_die.count(face) << '\n' ;
}

http://coliru.stacked-crooked.com/a/733c8fa2f0429b4e

C++ 11: http://en.cppreference.com/w/cpp/numeric/random
Thanks, this is exactly what I needed. I have been struggling for the longest time with how to use arrays in a class. This helped a lot.
Topic archived. No new replies allowed.