Concatenate a String?

This is a discussion on Concatenate a String? within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I have this code, but the last line seems to have a problem with the '+'. I thought ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    18

    Question Concatenate a String?

    Hey everyone,

    I have this code, but the last line seems to have a problem with the '+'. I thought to concatenate a string, you use the '+' operator.

    Code:
    int nMajorVersion = 1; // Major Version
    int nMinorVersion = 0; // Minor Version
    int nBuildVersion = 0; // Build Version
    char szFullVersion[5] = nMajorVersion + "." + nMinorVersion + "." + nBuildVersion;
    Any idea's where I am going wrong? The compiler tells me that it cannot add pointers?
    Thanks in advanced .

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't add an int, a string, an int, a string, and an int to get a string. You especially can't add all that together to get a char[6]!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    You can't add an int, a string, an int, a string, and an int to get a string. You especially can't add all that together to get a char[6]!
    Well you could at least tell me how I can. So basically, I would need to convert an int to a char?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Sorry, I got myself tangled with the other "hey I've got a bunch of different things and want a string" thread. So, you can make a stringstream and "print" all these things out to it (that seems to be the normal method), or you can use boost's lexical_cast to cast the ints to a string, I think.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're using C++ then why not USE C++.
    Use std::string instead of char arrays.

    You can use std::stringstream to combine all that into one big string:
    Code:
    #include <sstream>
    #include <string>
    ...
    std::stringstream ss;
    ss << nMajorVersion << "." << nMinorVersion << "." << nBuildVersion;
    std::string str = ss.str();
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Right guys, I don't know if you're insulting me or anything, but it's not nice. I am new to C++, and just want to ask.

    "hey I've got a bunch of different things and want a string" thread.
    Don't know if that was an insult. In VB.NET, it's similar to that, except you would convert the integers to strings.

    If I can't ask a question, then I wont bother; simple as.

    As to your good answers though - thanks.

    Cheers.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sharkbate24 View Post
    Right guys, I don't know if you're insulting me or anything, but it's not nice. I am new to C++, and just want to ask.



    Don't know if that was an insult. In VB.NET, it's similar to that, except you would convert the integers to strings.

    If I can't ask a question, then I wont bother; simple as.
    What it means is that if you go back to the C++ main page and look down one whole entire thread, to the one entitled "convert string to char" you would see pretty much the exact same question (albeit in mirror-image) being posted at pretty much the exact same time.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    What it means is that if you go back to the C++ main page and look down one whole entire thread, to the one entitled "convert string to char" you would see pretty much the exact same question (albeit in mirror-image) being posted at pretty much the exact same time.
    Oh ok. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Question on string concatenate (strcat)...
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2005, 11:42 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21