Thread: string concat

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    string concat

    Hey,

    I'm trying to concatenate strings with const chars (letters in the double quotes). I've tried using the + way, the sprintf, and strcat. None work... An example piece below...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ( int argc, char *argv[])
    {
         string word1 = "this is a string";
         string word2;
    
         word2 = "hello" + word1 + "\n";
    
         return 0;
    }
    ***not my actual code, it's just extremely similar to it***

    That's what I'm trying to do... can anyone help (i don't want to use c-strings)

    Thanks in advance,

    Guitarist809
    Last edited by guitarist809; 12-14-2007 at 04:49 PM.
    ~guitarist809~

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about something which compiles first?
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Salem View Post
    How about something which compiles first?
    ...?

    I don't know if that'll compile, I just wrote it because my code is long, this is just short and simple. Should be almost the same thing though. trying to do string += "text" + anotherstring;
    ~guitarist809~

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's nothing wrong with the code you currently have posted. Why do you think it doesn't work?

    If you do "some" + "text" where you have back to back string literals, it might not work, but that's just a guess of a common problem.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Fixed - code I had was bad in many ways
    Last edited by guitarist809; 12-14-2007 at 05:06 PM.
    ~guitarist809~

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    spaces_left is an int. You can't add an int to a string with operator+.

    What you want to use is a stringstream. Then you can concatenate everything together with operator<< and get the string from it when you are done.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Daved View Post
    spaces_left is an int. You can't add an int to a string with operator+.

    What you want to use is a stringstream. Then you can concatenate everything together with operator<< and get the string from it when you are done.
    Yep

    Thanks Daved!
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM