How do I output my code correctly?

Hey Yall, So I have two questions:

One, I am working on a 2d array to initialize it to an enum value and print it out. However, I don't know how to print out the value correctly. My current output looks like this.

1
2
3
4
5
6
7
8
9
10
*****
*****
*****
*****
*****
*@X@

@@
@└ö@
@ m


I am trying to get it to look something(not exactly because it is randomized) like this:
1
2
3
4
5
6
**@**
**@@**
*****
**@**
@****
*****


Here is my code
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

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

const int ROWS = 5; // a constant of row 
const int COLS = 5; // a constant of column. I don't want the board to be more than 5 X 5

//enum to represent every possible value in the board.
enum Symbols {STAR = 42, AT = 64, X = 88, ZERO = 0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE}; 

//Function to cout basic info (Rules, names etc..)
void info();

void initPosition(Symbols symbolArr[][COLS], int size); 
bool checkMine(Symbols symbolArr[][COLS], int size);
/*
display the initial board showing the initial values (i.e., ‘*’ (STAR)) and
randomly placed mines (i.e., ‘@’(AT)) using a function, passing in the two-dimensional array
and the size. This function will display the row and column headers for the board as well
as the board itself. Note that since this two-dimensional array is an
enumerated data type, I will have to map the enum value to the representative
character that is to be displayed on the board. 
*/void displayBoard(Symbols symbolArr[][COLS], int size);

	
int main()
{
	int mines;
	
	Symbols symbolArr[][COLS] = {STAR, AT, X, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
	
	do
	{
		cout << "Enter The Numbers of Mines Between 5 and 10: ";
		cin >> mines;
	}while(mines <= 4 || mines >=11);
	
	
	initPosition(symbolArr, ROWS);
	checkMine(symbolArr, mines);
	displayBoard(symbolArr, ROWS);

	
	
	
	return 0;
}

void initPosition(Symbols symbolArr[][COLS], int size) 
//  This function will simply initialize each position on the board to the enumerated type representing the initial value, which is the character ‘*’ (or STAR)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			//I need help here. I want to initalize this function to fill this array with enum star.
		}
		cout << endl;
	}
}

bool checkMine(Symbols symbolArr[][COLS], int size) // This function randomly selects arrays to place mines
{
	srand(time(NULL));  //Random Number generator

	
	for (int mines_placed = 0; mines_placed < size;)
	{
		int row = rand() % size;  
		int col = rand() % size;

		if (symbolArr[row][col] != AT)
		{
			symbolArr[row][col] = AT; 
			mines_placed++;
		}
		else
		{
			return false; 
		}

	}
	return true;
}

void displayBoard(Symbols symbolArr[][COLS], int size) // This function displays the board.
{

	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			cout <<(symbolArr[i][j]) << ""; // I need help here too (I think)
		}
		cout << endl;
	}

	return;		
}
Last edited on
I don't know why, but my code keeps crashing my compiler. I am using a g++ compiler on notepad++ and using cmd to open the file and when I put a number, it outputs the enums in number form, but it says program crashed as well

1
2
3
4
5
42648801
234564
78900
646422336419910464224204199021
642230018779941442797568641


I tried using similar code to the other user, but its just not working. My problem is that I don't know how to initalize a 2-d array. If it was a single array, then i could just go: arrayName[position], but i don't know what to do when there is a 2-d array.
Last edited on
Topic archived. No new replies allowed.