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?
How should I fix this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
}
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";
}
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.
I want to copy each character into another array of char.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :-)
> 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 :-)
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?