Programming in C++: 3D array string copy

In summary: Using a struct for the cards is a great idea and will make your code more organized and efficient. Good luck with your project!
  • #1
Dembadon
Gold Member
659
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
  • #2
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);
      }
}
 
  • #3
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
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!
 
  • #5


I would recommend breaking down the problem into smaller parts and debugging each part separately. First, ensure that your strCopy function is working correctly by testing it with a simpler 2D array. Once you have confirmed that it is working correctly, then you can move on to implementing it for a 3D array.

Additionally, make sure that you are passing the correct data types to your strCopy function. In this case, it seems like the error is caused by trying to assign a single character to an entire array. You may need to use a nested loop to assign each character individually to the correct location in the 3D array.

Another suggestion would be to use the string library in C++ to make the copying process easier. You can use the strcpy function to copy a string from one array to another. You can also use the strlen function to determine the length of a string, which can be helpful in your loop condition.

Overall, make sure to thoroughly test and debug your code, and don't be afraid to ask for help from your peers or a teacher if you are still having trouble. Good luck with your project!
 

1. What is a 3D array in C++?

A 3D array in C++ is a data structure that allows for the storage and manipulation of data in three dimensions. It is similar to a 2D array, but with an additional dimension. This means that the data can be organized in rows, columns, and levels.

2. How do you declare and initialize a 3D array in C++?

To declare and initialize a 3D array in C++, you can use the following syntax: datatype array_name[size1][size2][size3]; The datatype specifies the type of data that will be stored in the array (e.g. int, float, string), and the size1, size2, and size3 define the dimensions of the array.

3. What is the purpose of using strings in a 3D array in C++?

Strings in a 3D array are useful for storing and manipulating text data in three dimensions. This can be helpful in applications such as 3D graphics, where text needs to be displayed on different levels or layers. It can also be used for organizing and managing large amounts of text data.

4. How do you copy a string into a 3D array in C++?

To copy a string into a 3D array in C++, you can use the strcpy() function. This function takes two arguments - the destination array and the source string - and copies the contents of the string into the specified array. It is important to ensure that the destination array is large enough to hold the copied string.

5. Can you provide an example of using a 3D array to store and manipulate strings in C++?

Yes, here is an example of declaring, initializing, and copying a string into a 3D array in C++:

char names[2][3][10]; // declare a 3D array with 2 rows, 3 columns, and a string length of 10strcpy(names[0][0], "John"); // copy the string "John" into the first row and column of the arraystrcpy(names[0][1], "Mary"); // copy the string "Mary" into the first row and second column of the arraystrcpy(names[1][0], "Bob"); // copy the string "Bob" into the second row and first column of the arraystrcpy(names[1][1], "Lisa"); // copy the string "Lisa" into the second row and second column of the array

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
846
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
8K
Back
Top