itoa

This is a discussion on itoa within the C++ Programming forums, part of the General Programming Boards category; descrition says this function could Convert integer to string but actually it convert integer to char array, which brings problem ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    itoa

    descrition says this function could Convert integer to string but actually it convert integer to char array, which brings problem when handling it with other strings, like this:

    Code:
    int i = 9;
    char buffer[100];
    itoa (i, buffer, 10);
    string s = "it is a ";
    s += buffer;
    this wouldnt work because s and buffer belong to different type, how to solve this problem ???
    Never end on learning~

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Your code is correct,but not the best way to do this in C++.
    And itoa is not C++ Standard,or C Standard.

    Use stringstreams
    Code:
    #include <sstream>
    #include <string>
    ...
    int i = 9;
    std::stringstream stream;
    stream << i;
    std::string s = "it is a ";
    s += stream.str();
    Last edited by Wledge; 06-30-2003 at 02:50 AM.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Wledge
    Your code is correct,but not the best way to do this in C++.
    And itoa is not C++ Standard,or C Standard.

    Use stringstreams
    Code:
    #include <sstream>
    #include <string>
    ...
    int i = 9;
    std::stringstream stream;
    stream << i;
    std::string s = "it is a ";
    s += stream.str();
    oh it is pretty cool, and where could i find reference on this skill plz?
    Never end on learning~

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    i tried your code but that stringstream didnt work...
    Never end on learning~

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    oh it is pretty cool, and where could i find reference on this skill plz?
    The one, the only, the FAQ .
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by ripper079
    The one, the only, the FAQ .
    thanx i'll try this way.
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-19-2005, 11:44 PM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 08:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM

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