Thread: Concatenating function not working right?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    9

    Concatenating function not working right?

    I have a function that concatenate the strings in an array(2D)
    Ex 1: Sean Connery Micheal King James Wood
    Result: SeanConnery MichealKing JamesWood ...
    The concatenation function working correctly and displays correctly in the function.
    But if I make another function to display it, it shows this

    Ex 2: SeanConnery Sean MichealKing Micheal JamesWood James..
    It adds to first name. Why?

    Code:
    void Concatenation( char dest[200][13] )
      {
          // loop through and concatenation the strings
        for(int i=0;i<200;i+=2)
         {
          myStrCat(dest[i],dest[i+1]); // mystrcat is equalto strcat()
          
          cout << dest[i] << " "; // display correctly Ex 1
         }
      
     }
    Code:
    void display(  char dest[200][13] ){
       // Initialize variables
       int i;
       
       for( i = 0; i < 208; i++ )
        {
          
          cout << dest[i] << " "; // display wrongly displays Ex2
        }
    }

  2. #2
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10
    Quote Originally Posted by defjamvan View Post
    I have a function that concatenate the strings in an array(2D)
    Ex 1: Sean Connery Micheal King James Wood
    Result: SeanConnery MichealKing JamesWood ...
    The concatenation function working correctly and displays correctly in the function.
    But if I make another function to display it, it shows this

    Ex 2: SeanConnery Sean MichealKing Micheal JamesWood James..
    It adds to first name. Why?

    Code:
    void Concatenation( char dest[200][13] )
      {
          // loop through and concatenation the strings
        for(int i=0;i<200;i+=2)
         {
          myStrCat(dest[i],dest[i+1]); // mystrcat is equalto strcat()
          
          cout << dest[i] << " "; // display correctly Ex 1
         }
      
     }
    Code:
    void display(  char dest[200][13] ){
       // Initialize variables
       int i;
       
       for( i = 0; i < 208; i++ )
        {
          
          cout << dest[i] << " "; // display wrongly displays Ex2
        }
    }
    try declaring dest[200][13] globally or make it 'static'

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Suhasa010 View Post
    try declaring dest[200][13] globally or make it 'static'
    Why?

    OP -- I ran your code with:
    Code:
       char dest[200][13] = {"Sean", "Connery", "Micheal", "King", "James", "Wood"};
    and got:
    Code:
    SeanConnery Connery MichealKing King JamesWood Wood
    So not quite the same as you but close enough.
    It looks like you're misunderstood what strcat() does. strcat(str1, str2) concatenates a copy of str2 onto the end of str1. It doesn't change str2 in any way. So assuming "MyStrCat" really is the same as strcat:
    Code:
       myStrCat(dest[i],dest[i+1]);
    If dest[i] contains "Sean" and dest[i+1] contains "Connery", then after the strcat, dest[i] will contain "SeanConnery" and dest[i+1] will still contain "Connery". So you need to either have i += 2 as your loop increment in display(), or you need to reorganise your array to exclude the single names. If that's what you want then you'll need to copy the full names down so they're in consecutive elements, and probably zero out the rest of the elements. I'm not sure what you're trying to do though, as you still have a 200 (well, 208, typo?) loop terminator in display.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > cout << dest[i] << " "; // display correctly Ex 1
    You should try printing say
    cout << i << "=" << dest[i] << " "; // display correctly Ex 1

    Now do the same in your display function - print the subscript associated with each string.

    > for( i = 0; i < 208; i++ )
    Not overflowing the bounds of your array would be a good idea as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    It works! Thanks smokeyangel. I just had to output i+=2. One more thing, If I wanted to only concatenate the first 4 string and leave the last two alone how would i do it?
    Result: SeanConnery MichealKing James Wood. ie, "James" and "Wood" are left concatenate but they are still included in the output. so i < 200 doesn't change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if function not working (its inside anot if function)
    By ahahsiol in forum C Programming
    Replies: 2
    Last Post: 03-08-2013, 08:55 AM
  2. concatenating variables
    By who_cares in forum C Programming
    Replies: 5
    Last Post: 01-03-2006, 12:16 PM
  3. concatenating more than 2 strings
    By Grunt12 in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 05:31 PM
  4. concatenating two strings
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 08:27 PM
  5. Concatenating characters
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 08-22-2003, 09:48 PM