1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming in C++: 3D array string copy

  1. Sep 9, 2012 #1

    Dembadon

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations

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

    Code (Text):
    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 (Text):

    char emptyRow[ MAX_STR_LEN ] = "empty";
    char tableau[ MAX_ROWS ][ MAX_COLS ][ MAX_STR_LEN ];
     
    Here's how it is called:
    Code (Text):

    strCopy( tableau, emptyRow );
     
    3. The attempt at a solution

    Code (Text):

    // 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.
     
  2. jcsd
  3. Sep 10, 2012 #2

    Mark44

    Staff: Mentor

    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 (Text):
    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 (Text):

    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);
          }
    }

     
     
  4. Sep 11, 2012 #3

    Dembadon

    User Avatar
    Gold Member

    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.
     
  5. Sep 11, 2012 #4

    Mark44

    Staff: Mentor

    Sure, you're welcome, and glad to hear that it worked for you.
    Sounds good!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Programming in C++: 3D array string copy
  1. 3d vectors question (Replies: 1)

Loading...