Initiating and printing dynamic 2D array of characters

I'm trying to iterate through a dynamically allocated 2d array of characters and initialize all of the spaces to a blank. When I attempt to do this in the constructor, I get the error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive] from *showMap[i][j] = ' '; and I'm not exactly sure how to fix this. Additionally, I know I'm supposed to allocate memory for pointers and such when I declare them but I've never done it for a 2d array before, and I was wondering if I'm using the 'new' keyword correctly in this case or if I'm allocating memory correctly at all? Each element in the array is supposed to store 1 character.

I was also wondering if someone could look over my print statement? It should basically just print all the elements to the screen in this sort of style:

1
2
3
4
|H         |
|  T       | 
|          |
|         C|


Most elements will be blank, but it should sort of be presented in a 10x30 square with |'s fenced on both sides. The characters that ARE filled in represent runners in a marathon, and the runner who gets to the bottom right corner first wins.

I appreciate any help!! As a side note, I was wondering if someone could explain if I did my destructor correctly? I searched online for how to destruct a dynamic 2D array of characters and ended up with what's below, but I don't understand why I iterate and delete through MAX_ROWS only and not MAX_COLS as well.

View.cc:
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>
	using namespace std;
	#include <string>

	#include "View.h"

	View::View(){
		int i, j;
		for (i = 0; i < MAX_ROWS; ++i){
			for (j = 0; j < MAX_COLS; ++j){
				*showMap[i][j] = new char[1];
				*showMap[i][j] = ' ';
			}
		}
	}

	View::~View(){
		int i;
		for (i = 0; i < MAX_ROWS; ++i) {
		 delete showMap[i];
	  }
	  delete [] showMap;
	}

	void View::print(){
		cout << "|";
		for(int i = 0; i <= MAX_ROWS; i++){
			for(int j = 0; j <= MAX_COLS; j++){
				printf("%c", showMap[i][j]);
			}
			cout << "|";
		}
	}


View.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	#ifndef VIEW_H
	#define VIEW_H
	#define MAX_ROWS 10
	#define MAX_COLS 30

	#include <iostream>
	#include <string>

	#include "Position.h"
	using namespace std;

	class View{
	  public:
		View();
		~View();
		void print();
	  private:
		char** showMap[MAX_ROWS][MAX_COLS];
	};

	#endif 
Last edited on
First, decide whether you want to statically or dynamically allocate your 2D array.
You have a mish-mash of both ideas at the moment.

Static
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
class View{
  public:
	View();
	~View();
	void print();
  private:
	char showMap[MAX_ROWS][MAX_COLS];
};
View::View(){
	int i, j;
	for (i = 0; i < MAX_ROWS; ++i){
		for (j = 0; j < MAX_COLS; ++j){
			showMap[i][j] = ' ';
		}
	}
}

View::~View(){
	// nothing to do here
}

void View::print(){
	cout << "|";
	for(int i = 0; i < MAX_ROWS; i++){  //!! <= changed to <
		for(int j = 0; j < MAX_COLS; j++){
			printf("%c", showMap[i][j]);
		}
		cout << "|";
	}
}


Dynamic
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
class View{
  public:
	View();
	~View();
	void print();
  private:
	char **showMap;
};
View::View(){
	int i, j;
	showMap = new char*[MAX_ROWS];
	for (i = 0; i < MAX_ROWS; ++i){
		showMap[i] = new char[MAX_COLS];
	}
	for (i = 0; i < MAX_ROWS; ++i){
		for (j = 0; j < MAX_COLS; ++j){
			showMap[i][j] = ' ';
		}
	}
}

View::~View(){
	for (int i = 0; i < MAX_ROWS; ++i){
		delete [] showMap[i];
	}
	delete[] showMap;
}

void View::print(){
	cout << "|";
	for(int i = 0; i < MAX_ROWS; i++){  //!! <= changed to <
		for(int j = 0; j < MAX_COLS; j++){
			printf("%c", showMap[i][j]);
		}
		cout << "|";
	}
}


Hello marystew,

I hope this will go along with and add to what salem c has said.

Consider this:
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
#ifndef VIEW_H
#define VIEW_H

#define MAX_ROWS 10
#define MAX_COLS 30

//#include <iostream>  // <--- If you include this header at the right time these are not need, but OK if you feel they are needed. Otherwise they are not needed in this file.
//#include <string>

//#include "Position.h"  // <--- You did not provide this header file.

//using namespace std;  // <--- This is bad enough, but NEVER put in a header file.
// http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

class View
{
    public:
        View();
        ~View();
        void print();

    private:
        char** showMap[MAX_ROWS][MAX_COLS];  // <--- Is this a pointer to a pointer or a char array? One or the other, but not both.
};

#endif 

If the comments do not explain something let me know.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class View
{
   vector<string> showMap;
   int ROWS, COLS;
public:
   View( int r = 10, int c = 10 ) : ROWS( r ), COLS( c ){ showMap = vector<string>( r, string( c, ' ' ) ); }
   void set( int i, int j, char c ){ if ( i >= 0 && i < ROWS && j >= 0 && j < COLS ) showMap[i][j] = c; }
   void print(){ for ( auto &s : showMap ) cout << '|' << s << '|' << '\n'; }
};

int main()
{
   View V( 4, 10 );
   V.set( 0, 0, 'H' );
   V.set( 1, 1, 'T' );
   V.set( 3, 9, 'C' );
   V.print();
}

|H         |
| T        |
|          |
|         C|
Last edited on
Topic archived. No new replies allowed.