Taking in Two C-style strings and placing it into one 2-d array?

I have an assignment where we need to place two words (separated by a space) into a 2-d array. Since the words are not separated by anything, I thought taking in two separate c-style strings and then concatenating then into a separate 2-d array would work? But I keep getting several errors or crashes when I try to do this. Any input would help.
An example of the file would be "ten heart jack heart queen heart king heart" Where you need to have "ten heart" as a single value in the 2-d array, and they are not separated by commas.

void getData(const char fileName[], char data[][MAX_COL])
{

ifstream fileInStream;
int indexRow=0;
int indexCol = 0;
char suit[40];
char number[40];
// (MAX_COL is set to 2 right now)



fileInStream.clear();
fileInStream.open(fileName);


// while(fileInStream.good())
// {
for(indexRow=0; indexRow < 4; indexRow++)
{

for(indexCol=0; indexCol < MAX_COL; indexCol++)
{


fileInStream >> suit;
fileInStream >> number;

cout << suit << " " << number << endl;


//the program seems to successfully take in the suit
and number, but then when I try to place these two items
into a single 2-d array I get errors.

//these are a few examples below I have tried with no success

strcpy(data[indexCol], suit);
strcpy(*data, suit);
strcat(*data, suit);

}

}


fileInStream.close();

}


Thanks for the input.
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

SamualTP wrote:
Since the words are not separated by anything, I thought taking in two separate c-style strings and then concatenating then into a separate 2-d array would work?
Why not std::string and no arrays at all? In general you should avoid using c-style strings as they are a pain to work with and in fact are very dangerous.
Last edited on
Topic archived. No new replies allowed.