Returning a dynamic array from a function?

Hi everyone,

Apologies for the possible noob question, but I am having a hard time understanding what I need to do in order to pass a dynamic array from a function. The project below is a dice game I am working on. I apologize if the code is a bit messy but what I would like to do is take the diceArray that is created within my returnDiceF function and use it within the callOutF function.

Amy assistance is greatly appreciated!

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

void gameRulesF();
int numOfPlayersF();
int numOfRoundsF();
int returnDiceF(int numOfPlayers);
bool callOutF(int currentPlayer, int numOfPlayers);
int playerGuessF(int currentPlayer, int numOfDice);
void gameLoopF(int numOfPlayers, int numOfRounds, int numOfDice);

int main()
{

	// Variable declarations
	int numOfPlayers, numOfRounds, numOfDice;
	int guess1Check, guess2Check;	// Checks against dice in array?

	gameRulesF();

	numOfPlayers = numOfPlayersF();

	numOfRounds = numOfRoundsF();

	numOfDice = returnDiceF(numOfPlayers);

	gameLoopF(numOfPlayers, numOfRounds, numOfDice);

	return 0;
}

void gameRulesF() {	// Displays Welcome message and game rules to the players

	string line;

	ifstream infile("Rules.txt");

	if (infile.is_open()) {

		while (getline(infile, line)) {

			cout << line << '\n';

		}

		infile.close();

	}
	else {

		cout << "Unable to open file";

	}

	cout << endl;

}

int numOfPlayersF() {	// Checks for number of players in the game and returns error if less than 2 players are in the game

	int numOfPlayers;

	cout << "Enter the number of players: ";
	cin >> numOfPlayers;

	while (numOfPlayers < 2) {

		cout << "You must add an additional player" << endl;
		cout << "Enter the number of players: ";
		cin >> numOfPlayers;

	}

	return numOfPlayers;

}

int numOfRoundsF() {	// Checks for the number of rounds to be played. If less than 1 round is played an error is returned.

	int numOfRounds;

	cout << "Enter the number of rounds to play: ";
	cin >> numOfRounds;

	while (numOfRounds < 1) {

		cout << "You must add an additional round" << endl;
		cout << "Enter the number of rounds: ";
		cin >> numOfRounds;

	}

	return numOfRounds;

}

int returnDiceF(int numOfPlayers) {	// Returns 5 dice per player to the game

	int numOfDice = numOfPlayers * 5;

	cout << "Returning " << numOfDice << " dice to the game..." << endl << endl;

	srand(time(NULL));

	static int *diceArray = new int[numOfDice];

	for (int i = 0; i < numOfDice; i++) {

		diceArray[i] = (rand() % 6) + 1;

	}

	return numOfDice;
}

int playerGuessF(int currentPlayer, int numOfDice) {	// Allows player to guess dice and return error if the guess is invalid

	int guess1, guess2;

	cout << "Player " << currentPlayer << "'s turn..." << endl << endl;

	cout << "Enter a number of dice to guess: ";
	cin >> guess1;

	while (guess1 < 1 || guess1 > numOfDice) {

		cout << "Invalid guess" << endl;
		cout << "Re-enter your first guess: ";
		cin >> guess1;

	}

	cout << "Enter the face value of the dice: ";
	cin >> guess2;

	while (guess2 < 1 || guess2 > 6) {

		cout << "Invalid guess" << endl;
		cout << "Re-enter your second guess: ";
		cin >> guess2;

	}

	cout << "You guessed " << guess1 << " dice that are " << guess2 << "'s" << endl;

	return guess1, guess2;

}

bool callOutF(int currentPlayer, int numOfPlayers) {

	string answer = "no";
	bool calledOut = false;

	do {

		cout << "Player " << currentPlayer + 1 << ": " << "do you choose to call out? (Y or N): ";
		cin >> answer;

		if (answer == "Y" || answer == "y") {

			calledOut = true;

		}
		else if (answer == "N" || answer == "n") {

			currentPlayer++;
			calledOut = false;

		}
		else {

			cout << "Invalid answer" << endl;
			cout << "Re-enter your answer: ";
			cin >> answer;

		}

	} while (currentPlayer < numOfPlayers);

	return calledOut;

}

//void guessCheckF() {
//
//	for (int i = 0; i < diceArray; i++) {
//
//		if (diceArray[i] = guess2 &&)
//	}
//
//}

void gameLoopF(int numOfPlayers, int numOfRounds, int numOfDice) {

	int currentPlayer = 1;

	int currentRound = 1;

	do {

		for (int i = 0; i < numOfPlayers; i++) {

			playerGuessF(currentPlayer, numOfDice);

			callOutF(currentPlayer, numOfPlayers);

		}

		currentRound++;

	} while (currentRound < numOfRounds);

}
Hi,

Consider that you really don't need a dynamic array - that is what std::vector is for :+) It implicitly stores it's data on the heap, and one can push_back more items.

Good Luck !!

http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.