• Support PF! Buy your school textbooks, materials and every day products Here!

Programming in C++: 3D array string copy

  • Comp Sci
  • Thread starter Dembadon
  • Start date
  • #1
Dembadon
Gold Member
624
89

Homework Statement



I am making a Klondike style solitaire game, and I am trying to load a string into a 3D array. I am representing the "Tableau" with a 3D array (we haven't gone over structs yet). I want to represent the spaces which don't have cards with the word "empty". I'm not understanding how to copy a C-style string to a 3D array.

Homework Equations



My compiler, g++, is giving me the following error:

Code:
uploadTest.cpp: In function ‘void strCopy(char (*)[60][80], char*)’:
uploadTest.cpp:240: error: incompatible types in assignment of ‘char’ to ‘char [80]’
Here are my array declarations:
Code:
char emptyRow[ MAX_STR_LEN ] = "empty";
char tableau[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ];
Here's how it is called:
Code:
strCopy( tableau, emptyRow );

The Attempt at a Solution



Code:
// prototype
void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] );

////////////////////////////////////////////////////////////////

// implementation

void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] ) {
	
	// initialize function/variables
	int colI = 0;
	int rowIndex = 0, colIndex = 0;
	
	// loop through source array
	while( srcArr[ colI ] != NULL_CHAR ) {
		
		// assign character
		destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];
		
		// increment index
		colI++;
	}
	
}
Do I have to create a third index for the destArr and increment it? I tried doing this but the compiler still complained.
 

Answers and Replies

  • #2
33,071
4,771

Homework Statement



I am making a Klondike style solitaire game, and I am trying to load a string into a 3D array. I am representing the "Tableau" with a 3D array (we haven't gone over structs yet). I want to represent the spaces which don't have cards with the word "empty". I'm not understanding how to copy a C-style string to a 3D array.

Homework Equations



My compiler, g++, is giving me the following error:

Code:
uploadTest.cpp: In function ‘void strCopy(char (*)[60][80], char*)’:
uploadTest.cpp:240: error: incompatible types in assignment of ‘char’ to ‘char [80]’
Here are my array declarations:
Code:
char emptyRow[ MAX_STR_LEN ] = "empty";
char tableau[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ];
Here's how it is called:
Code:
strCopy( tableau, emptyRow );

The Attempt at a Solution



Code:
// prototype
void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] );

////////////////////////////////////////////////////////////////

// implementation

void strCopy( char destArr[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ],
						char srcArr[ MAX_STR_LEN ] ) {
	
	// initialize function/variables
	int colI = 0;
	int rowIndex = 0, colIndex = 0;
	
	// loop through source array
	while( srcArr[ colI ] != NULL_CHAR ) {
		
		// assign character
		destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];
		
		// increment index
		colI++;
	}
	
}
Do I have to create a third index for the destArr and increment it? I tried doing this but the compiler still complained.
Instead of copying characters one by one, which appears to be what you think you're doing, why not use the strcpy C standard library function?

Here's where your error is coming from, I believe.
Code:
destArr[ rowIndex ][ colIndex ] = srcArr[ colI ];
The problem is a mismatch of types. On the left side, destArr[ rowIndex ][ colIndex ] is an address, the address of the first character in the string it will hold. On the right side is a character. This is what your compiler error message is saying.


Also, since your tableau array is essentially a two-dimensional array of strings, I would use an outer for loop to iterate through the rows, and an inner for loop to iterate through the columns in a particular row. Presumably you know how many rows and columns there will be.

Here's how I would do what you're trying to do. Notice that I added a couple of parameters for the number of rows and columns of the tableau. This should work, but I haven't had time to test it.

Code:
void strCopy( char destArr[ ][ ], srcArr[ ], int rows, int cols )
{
   int i, j;
   for (i = 0; i < rows, ++i)
   {
      for (j = 0; j < cols; ++j)
      {
         strcpy(destArr[i][j], srcArr);
      }
}
 
  • #3
Dembadon
Gold Member
624
89
Thank you for your help, Mark! I now understand my error with array assignments, and the strCopy function you provided worked.

Additionally, we went over structs today in class, and I think I can already find a good use for them in this program. I'm planning on making a "card" struct, which will hold three elements: rank, suit, color. I can then declare a deck array of size 52, load it from a file, and use then use it to populate my tableau. I also don't think I'll need a 3D array for the tableau anymore, since I should just be able to load the tableau at a given row and column using my newly created deck array.
 
  • #4
33,071
4,771
Thank you for your help, Mark! I now understand my error with array assignments, and the strCopy function you provided worked.
Sure, you're welcome, and glad to hear that it worked for you.
Additionally, we went over structs today in class, and I think I can already find a good use for them in this program. I'm planning on making a "card" struct, which will hold three elements: rank, suit, color. I can then declare a deck array of size 52, load it from a file, and use then use it to populate my tableau. I also don't think I'll need a 3D array for the tableau anymore, since I should just be able to load the tableau at a given row and column using my newly created deck array.
Sounds good!
 

Related Threads for: Programming in C++: 3D array string copy

Replies
4
Views
12K
Replies
2
Views
908
Replies
3
Views
3K
  • Last Post
Replies
5
Views
540
Replies
3
Views
1K
  • Last Post
Replies
8
Views
1K
Replies
21
Views
2K
Replies
6
Views
7K
Top