I'm making a memory game for my C++ class. I need some help assigning the card character array to the grid array. Right the grid has ? as filler which should be swapped with one of the characters. However, when I compile it's giving me something like f?eb?hfdc?eg??hc. How do I get all the values to assign to a grid spot? It keeps leaving like 4 question marks in the array.
const int ROWS = 4;
const int COLS = 4;
int generate_rand_num(int min, int max);
void read_and_validate(int& input, int min, int max);
void display_grid(char grid_array[ROWS][COLS], bool mask_array[ROWS][COLS]);
int main() {
srand(time(NULL));
char grid[ROWS][COLS] = {
{
'?',
'?',
'?',
'?'
},
{
'?',
'?',
'?',
'?'
},
{
'?',
'?',
'?',
'?'
},
{
'?',
'?',
'?',
'?'
}
};
char cards[] = {
'a',
'a',
'b',
'b',
'c',
'c',
'd',
'd',
'e',
'e',
'f',
'f',
'g',
'g',
'h',
'h'
};
// Assigning cards to grid.
for (int n = 0; n < 16; n++) {
int i = generate_rand_num(0, 4);
int j = generate_rand_num(0, 4);
if (grid[i][j] == '?') {
grid[i][j] = cards[n];
} else {
i = generate_rand_num(0, 4);
j = generate_rand_num(0, 4);
grid[i][j] = cards[n];
}
}
for (int i=0; i <4; i++) {
for (int j=0; j<4; j++){
cout << grid[i][j];
}
}
return 0;
}
// Generates random number between 0 and 3.
int generate_rand_num(int min, int max) {
int number = (rand() % max) + min;
return number;
}