Results 1 to 7 of 7

Thread: How to concatenate 2 words in VC++?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I've read through some examples that show how to concatenate strings but can't seem to get any of them to work in a Win32 app.

    What I would like to do is the following:

    char* val1;
    char* val2;

    MessageBox(hDlg,val1 + " " + val2,"Info",MB_OK);

    Now I know that wont work as it is but I just wanted to make it clear what I wanted to do..

    I'm not using MFC.

    Any help and examples would be greatly appreciated..

    Dan

    Visual Studio 2010

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    You could use the
    lstrcat api:

    Code:
    MessageBox(hDlg,lstrcat(val1, val2),"Info",MB_OK);
    But if you include string you could also concatenate using the + you provided.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Guest
    Code:
    #include <string.h>
    
    .
    .
    .
    
    char buffer1[] = "Hello";
    char buffer2[] = "You";
    strcat(buffer1, ", ");
    strcat(buffer1, buffer2);
    
    cout<<buffer1<<endl;

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <string>
    
    using std::string;
    
    ...
    
    char *one = "Hello";
    char *two = "World";
    
    MessageBox(NULL, string(one) + " " + two, "Message", MB_OK);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    He already uses the MessageBox API call, so he probably already has
    #include windows.h

    so why not use the
    lstrcat from the windows.h?
    Saves you one header file
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You have to be careful not to get inconsistencies between the way all the arrays are allocated. For example, there are 3 ways just off the top of my head:

    new / delete / delete[] (c++)
    malloc / free (c)
    GlobalAlloc / GlobalFree (windows)

    The strcat functions automatically extend the char* array - so if it uses a different allocation method to the one you originally used to make the string, then you'll get a GPF
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks guys! ChimpFace9000's code works but I can't seem to get Jop's code to work.. On Jop's, it doesn't error out but it just doesn't show the MessageBox when I put the lstrcat(val1,val2) in there.. Oh well.. I would've liked to use that cause it's a lot cleaner..

    Visual Studio 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width