Comp Sci Programming in C++: 3D array string copy

Click For Summary
The discussion revolves around implementing a string copy function for a 3D array in a Klondike-style solitaire game using C++. The user encounters a compiler error due to a type mismatch when attempting to assign a character to a string array element. A suggested solution involves using the `strcpy` function to copy strings more effectively, along with modifying the function to accept the number of rows and columns. The user later realizes the potential of using structs for card representation, which could simplify the tableau structure and eliminate the need for a 3D array. This shift in approach enhances the game's design and functionality.
Dembadon
Gold Member
Messages
658
Reaction score
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.
 
Physics news on Phys.org
Dembadon said:

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);
      }
}
 
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.
 
Dembadon said:
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.
Dembadon said:
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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
19K
Replies
8
Views
3K