How to allow the user to "roll the dice" x times and save those values into a 2D array

Hey guys,

I am currently working on tweaking a project for my class where we have to allow the user to roll dice (or generate random numbers 1-6) for a total of 13 turns. I cannot generate all of the numbers at once (like save all values to a 2D array in one go) but I have to be able to save the values from the first turn (3 4 5 5 1), second turn (4 5 6 2 1) and so on separately.

How would I go about this? Here is the current code for this if you want to take a look. As you will see, I can only generate all of the numbers at once and save them to a 2D array:

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
void userRoll() {
    int dieRoll = 1;
    int userEntry = 0;
    int* ptrDieRoll = &dieRoll;

    while (userEntry != 2) {
        cout << "Turn #" << *ptrDieRoll << endl;
        cout << "   > 1: Roll the die" << endl;
        cout << "   > 2: Quit";
        cin >> userEntry;

        if (userEntry == 1) {//if user wants to roll die
            int tempArr[13][5];
            srand(time(0));
            for (int i = 0; i < 13; i++) {
                for (int j = 0; j < 5; j++) {
                    tempArr[i][j] = (rand() % 6) + 1;
                    cout << tempArr[i][j];
                }
                cout << endl;
            }
            dieRoll += 1;
        }
        else if (userEntry == 2) {
            cout << "Sorry to see you go. Goodbye!";
        }
    }
}


What should I do to prompt the user for every turn and only save that row to my array? Or is there a better way of doing this?
Last edited on
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 <ctime>

using namespace std;

const int NO_ROWS{13};
const int NO_COLS{6};

int main()
{
    int** array = new int*[NO_ROWS];
    for(int i = 0; i < NO_ROWS; ++i)
        array[i] = new int[NO_COLS];
    
    srand(time(nullptr));
    
    int dieRoll{0};
    int userEntry = 0;
    
    while (
           cout
           << "Turn #" << dieRoll << '\n'
           << "   > 1: Roll the die\n"
           << "   > 2: Quit\n"
           and cin >> userEntry
           and dieRoll < NO_ROWS
           )
    {
        switch (userEntry)
        {
            case 1:
                for (int i = 0; i < NO_COLS; i++)
                {
                    array[dieRoll][i] = (rand() % 6) + 1;
                    cout << array[dieRoll][i];
                }
                cout << endl;
                dieRoll++;
                break;
                
            case 2:
                cout << "Sorry to see you go. Goodbye!\n";
                return -999;
                
            default:
                cout << "** ERROR try again\n";
        }
    }
    
    // DISPLAY THE LOT
    for(int i = 0; i < NO_ROWS; i++)
    {
        cout << "Turn " << i + 1 << ": ";
        
        for(int j = 0; j < NO_COLS; j++)
        {
            cout << array[i][j] << ' ';
        }
        cout << '\n';
    }
    
    return 0;
}



Turn #0
   > 1: Roll the die
   > 2: Quit
1
235432
Turn #1
   > 1: Roll the die
   > 2: Quit
1
434164
Turn #2
   > 1: Roll the die
   > 2: Quit
1
621241
Turn #3
   > 1: Roll the die
   > 2: Quit
1
521236
Turn #4
   > 1: Roll the die
   > 2: Quit
1
234434
Turn #5
   > 1: Roll the die
   > 2: Quit
1
131125
Turn #6
   > 1: Roll the die
   > 2: Quit
1
162551
Turn #7
   > 1: Roll the die
   > 2: Quit
1
621413
Turn #8
   > 1: Roll the die
   > 2: Quit
1
345635
Turn #9
   > 1: Roll the die
   > 2: Quit
1
142656
Turn #10
   > 1: Roll the die
   > 2: Quit
1
661633
Turn #11
   > 1: Roll the die
   > 2: Quit
1
121444
Turn #12
   > 1: Roll the die
   > 2: Quit
1
332216
Turn #13
   > 1: Roll the die
   > 2: Quit
1
Turn 1: 2 3 5 4 3 2 
Turn 2: 4 3 4 1 6 4 
Turn 3: 6 2 1 2 4 1 
Turn 4: 5 2 1 2 3 6 
Turn 5: 2 3 4 4 3 4 
Turn 6: 1 3 1 1 2 5 
Turn 7: 1 6 2 5 5 1 
Turn 8: 6 2 1 4 1 3 
Turn 9: 3 4 5 6 3 5 
Turn 10: 1 4 2 6 5 6 
Turn 11: 6 6 1 6 3 3 
Turn 12: 1 2 1 4 4 4 
Turn 13: 3 3 2 2 1 6 
Program ended with exit code: 0
Thank you so much! This worked perfectly. Out of pure curiosity since I am a beginner in C++, would you mind explaining this bit of code to me:

1
2
3
 int** array = new int*[NO_ROWS];
    for(int i = 0; i < NO_ROWS; ++i)
        array[i] = new int[NO_COLS];


I have no idea what it does but when I change too much about it the entire thing stops working. I just want to become a better programmer and be knowledgeable about things like this.
It's creating a 2-d array. L1 creates an array of pointers to each of the columns. L2-3 creates a column array for each of the rows and sets the appropriate row pointer to point to its column.

The provided code is missing the code needed to delete each of the allocated arrays.
@OP

Yep, it creates a (dynamic) 2D array. Her is a handy reference that's been aound for a while or two.
https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

Of course, the delete isn't missing at all in my code because the array is finished with and cleaned up when the program exits, the delete is automatic. There are plenty of cases where delete need to be explicitly called and this is shown in the reference.

The sort of array construction I have used to suit your data model is one amongst many so there are no hard and fast rules or implementations.
@OP
And this would have done the job ... just as you had originally (except for the 5)
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 <ctime>

using namespace std;

const int NO_ROWS{13};
const int NO_COLS{6};

int main()
{
    int array[NO_ROWS][NO_COLS];
    
    srand(time(nullptr));
    
    int dieRoll{0};
    int userEntry = 0;
    
    while (
           cout
           << "Turn #" << dieRoll << '\n'
           << "   > 1: Roll the die\n"
           << "   > 2: Quit\n"
           and cin >> userEntry
           and dieRoll < NO_ROWS
           )
    {
        switch (userEntry)
        {
            case 1:
                for (int i = 0; i < NO_COLS; i++)
                {
                    array[dieRoll][i] = (rand() % 6) + 1;
                    cout << array[dieRoll][i];
                }
                cout << endl;
                dieRoll++;
                break;
                
            case 2:
                cout << "Sorry to see you go. Goodbye!\n";
                return -999;
                
            default:
                cout << "** ERROR try again\n";
        }
    }
    
    // DISPLAY THE LOT
    for(int i = 0; i < NO_ROWS; i++)
    {
        cout << "Turn " << i + 1 << ": ";
        
        for(int j = 0; j < NO_COLS; j++)
        {
            cout << array[i][j] << ' ';
        }
        cout << '\n';
    }
    
    return 0;
}
Last edited on
You might find this of use. You can specify the number of turns and the number of dice used on the command line. Why do you have to save the generated numbers - for just the output there's no need?

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
#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>

constexpr size_t MaxTurns {200};
constexpr size_t MaxDice {20};

int main(int argc, char* argv[]) {
	std::mt19937 rng(std::random_device {}());
	std::uniform_int_distribution<unsigned> distrib(1, 6);
	size_t noTurns {13};
	size_t noDice {5};

	if (argc == 2 || argc == 3) {
		noTurns = std::strtoull(argv[1], nullptr, 10);

		if (argc == 3)
			noDice = std::strtoull(argv[2], nullptr, 10);
	} else if (argc > 3)
		return (std::cout << "Usage: " << argv[0] << " <Turns> <Dice>\n"), 1;

	if (noTurns < 1 || noTurns > MaxTurns || noDice < 1 || noDice > MaxDice)
		return (std::cout << "Invalid parameters - max " << MaxTurns << " turns, " << MaxDice << " dice\n"), 2;

	const auto dice {new unsigned* [noTurns]};

	for (size_t t {}; t < noTurns; ++t)
		dice[t] = new unsigned[noDice];

	for (size_t t {}; t < noTurns; ++t)
		for (size_t d {}; d < noDice; ++d)
			dice[t][d] = distrib(rng);

	for (size_t t {}; t < noTurns; ++t) {
		std::cout << "\nTurn " << std::setw(3) << t + 1 << ": ";

		for (size_t d {}; d < noDice; ++d)
			std::cout << dice[t][d] << ' ';
	}

	std::cout << '\n';

	for (size_t t {}; t < noTurns; ++t)
		delete[] dice[t];

	delete[] dice;
}



c:\MyProgs>test64 13 6

Turn  1: 4 2 4 5 4 6
Turn  2: 5 6 1 4 3 4
Turn  3: 3 4 6 3 6 1
Turn  4: 1 1 3 6 5 3
Turn  5: 1 6 2 6 3 6
Turn  6: 3 1 4 2 3 4
Turn  7: 5 5 2 6 6 1
Turn  8: 4 5 3 6 5 2
Turn  9: 2 5 5 2 1 5
Turn 10: 3 5 2 3 2 5
Turn 11: 3 5 2 4 4 3
Turn 12: 4 2 3 5 4 4
Turn 13: 6 1 2 4 6 5

Last edited on
I needed to save the output so that I could plug it into a separate function taking in a 2d array that performs certain actions on each row. I don't think that I would have access to the numbers without saving them for later.

Thank you both for all of the help.
Last edited on
Topic archived. No new replies allowed.