#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0

    Problems with C++ itoa() function


    Hello:

    This is what I am trying to do, assign an integer to a string so for instance:

    int myInt=5;
    string myStr;

    myStr=myInt;

    and I want myStr to have "5". So I am using the "itoa()" function of the format:

    #include <cstdlib>

    char* itoa( int value,
    char* buffer,
    int radix );

    and I know that that I am implementing it properly. I am running two compilers, Metrowerks code warrior on windows and G++ on Linux, so when I compile the code on the windows machine it works fine but on the Linux machine (which is my first priority) I get this error:

    implicit declaration of function `int itoa(...)'

    What am I doing wrong? I googled quite a bit and found some people saying that it might be a bug, but that they werent sure. so is there a way that I can use that function in G++?

    Thanx!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    itoa() is a non-standard function. Use sprintf() instead :D. If you want to go the C++ way, you might also want to consider the strstream class
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0
    Code:
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        int MyInt = 5;
        string MyString;
        ostringstream MyStream;
    
        MyStream.seekp(0);
        MyStream << MyInt << ends;
        MyString = MyStream.str();
    
    }
    MyString should now hold 5

IMN logo majestic logo threadwatch logo seochat tools logo