`itoa' undeclared (first use this function)

I got this error when I run my c++ program in UNIX. I have no problem when running the program in windows.
How should I fix this?
icysmartyAsked:
Who is Participating?
 
pb_indiaConnect With a Mentor Commented:
Ok Its kinda same..use this..

#include <iostream>
#include <sstream>

static inline string IntTypeToStr(int x){
          std::ostringstream o;
          if (!(o << x))
               printf("Error in conversion from int to string \n");
          return o.str();
     }

int main()
{
     int i = 102345;
    char* temp = new char[1024];

       strcpy(temp,(char*)IntTypeToStr(i).c_str());
       delete[] temp;
}

//temp will be "10235"
0
 
SteHCommented:
have a look at
http://www.cplusplus.com/ref/cstdlib/itoa.html
This function is not ANSI C and thus only supported by some compilers. Could it be replaced with sprintf?
0
 
rstaveleyConnect With a Mentor Commented:
Try using ostringstream.

e.g.
--------8<--------
#include <iostream>
#include <sstream>

template<class T> std::string tostring(T x)
{
      std::ostringstream s;
      s << x;
      return s.str();
}

int main()
{
      int i = 1234;
      std::cout << "The number is "+tostring(i)+"\n";
}
--------8<--------
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
pb_indiaCommented:
itoa() is not supported on all UNix platforms natively. Example above by rstaveley is good to do this function,.

I always use the following -


#include <iostream>
#include <sstream>

static inline string IntTypeToStr(int x){
            std::ostringstream o;
            if (!(o << x))
                  printf("Error in conversion from int to string \n");
            return o.str();
      }

int main()
{
     int i = 10;
     std::cout << "The number is "+IntTypeToStr(i)+"\n";
}


0
 
icysmartyAuthor Commented:
I do not want to print it out. Instead I want to convert the integer into char* .
I want to copy each character into another array of char.

0
 
icysmartyAuthor Commented:
Yup, it works.
0
 
rstaveleyCommented:
Notice that character arrays and IOStreams/STL always look a bit clumsy?

> I want to copy each character into another array of char

It sounds like you are working with code in the C mindset rather than C++ mindset. Under those circumstances, I'd go for SteH's suggested sprintf.

        i = 1234;
        char buf[1024]
        sprintf(buf,"%d",i);

However, you should be feeling some sort of draw towards using std::string rather than those evil old character arrays and pointers :-)
0
All Courses

From novice to tech pro — start learning today.