Link to home
Start Free TrialLog in
Avatar of icysmarty
icysmarty

asked on

`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?
Avatar of Member_2_1001466
Member_2_1001466

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?
SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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";
}


Avatar of icysmarty

ASKER

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.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup, it works.
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 :-)