Thread: concatenating integer to string

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    585

    concatenating integer to string

    Hi guys!
    I'm facing a problem that I would to concatenate integer to string, so I did:
    Code:
    int count=5;
    string tmp= "";
    tmp+=count;
    it's wrong ! because the solution included also '0' in tmp+, mean:
    Code:
    int count=5;
    string tmp= "";
    tmp+=count+ '0';
    what does + '0' mean in that case? why I'm using that sign?

  2. #2
    Registered User
    Join Date
    Apr 2015
    Posts
    585
    any help please?

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    407
    Oh... maybe adding a '0' to an integer is to add an '0' to an integer? Just maybe?

  4. #4
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    66
    string isn't a data type. You want char* or char[n], which is an array of characters.

    You can try sprintf to put the int into the character array.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    407
    Quote Originally Posted by catacombs View Post
    string isn't a data type. You want char* or char[n], which is an array of characters.
    Nope... in STL there is a class called basic_string and an alias called string. They are in namespace std (you need to include string header)... Notice this question was posted in C++ Programming board (not C).

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    407
    RyanC, to help you answer your own question, here's a test:

    Code:
    $ g++ -xc++ -include iostream - <<EOF
    int main() { int x = '0'; std::cout << x << std::endl; }
    EOF
    $ ./a.out
    48
    Hope it helps.

  7. #7
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    66
    Quote Originally Posted by flp1969 View Post
    Nope... in STL there is a class called basic_string and an alias called string. They are in namespace std (you need to include string header)... Notice this question was posted in C++ Programming board (not C).
    :facepalm:

    Thanks. I didn't see the C++ tag.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    585
    Quote Originally Posted by flp1969 View Post
    RyanC, to help you answer your own question, here's a test:

    Code:
    $ g++ -xc++ -include iostream - <<EOF
    int main() { int x = '0'; std::cout << x << std::endl; }
    EOF
    $ ./a.out
    48
    Hope it helps.
    I understand that '0' is mapped to integer 48, but once again so 48+6=54 is an integer, and because I want it in char because I'm using "+" so my pc automatically mapping 54 as char which is '6'?!

    I'm confused of how he decide to take integer 54 or char '6' ?


    and if I understand you, if I write 6+48 instead of 6+'0' ..will I get on the output '6'?
    Last edited by RyanC; 05-24-2019 at 03:17 AM.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    407
    American Standard Code for Information Interchange (ASCII)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Integer to String and String to Integer
    By hqt in forum C++ Programming
    Replies: 26
    Last Post: 09-15-2011, 11:39 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. concatenating a string
    By simpatico_qa in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 04:06 PM
  4. concatenating a string with '\0's in it
    By yahn in forum C++ Programming
    Replies: 7
    Last Post: 02-12-2006, 01:24 AM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM

Tags for this Thread