Home > front end >  Merge two 2D arrays into one C
Merge two 2D arrays into one C

Time:07-28

I would like to merge two arrays (inventory & inventory2) into one new array (inventory3) for sorting purposes. I am unable to have inventory3 have values assigned by inventory2 (the second nested for loop). inventory3 is accepting assignment from inventory correctly. I cannot figure out why this is not working. I have been able to verify that the contents of ivnentory2 are correct.

void sortInput(string inventory[10][3], int inventory2[10][2]) {
  string inventory3[10][5];
  for (int count = 0 ; count < 10 ; count  ) {
    for (int count2 = 0 ; count2 < 3 ; count2  ) {
      inventory3[count][count2] = inventory[count][count2];
      cout << inventory3[count][count2] << "\t";
      }    
    for (int count2 = 3, count3 = 0 ; count3 < 2 ; count2  , count3  ) {
      inventory3[count][count2] = inventory2[count][count3];
      cout << inventory3[count][count2] << "\t";
      }
    cout << endl;
    }
}

CodePudding user response:

You're trying to assign an int to a string in this statement:

inventory3[count][count2] = inventory2[count][count3];

To make it work, convert the int to string like this:

inventory3[count][count2] = to_string(inventory2[count][count3]);
  •  Tags:  
  • c
  • Related