Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itoa function problem

I'm working on Eclipse inside Ubuntu environment on my C++ project.

I use the itoa function (which works perfectly on Visual Studio) and the compiler complains that itoa is undeclared.

I included <stdio.h>, <stdlib.h>, <iostream> which doesn't help.

like image 693
Aviadjo Avatar asked Sep 26 '10 20:09

Aviadjo


3 Answers

www.cplusplus.com says:

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

Therefore, I'd strongly suggest that you don't use it. However, you can achieve this quite straightforwardly using stringstream as follows:

stringstream ss;
ss << myInt;
string myString = ss.str();
like image 130
Component 10 Avatar answered Nov 15 '22 08:11

Component 10


itoa() is not part of any standard so you shouldn't use it. There's better ways, i.e...

C:

int main() {
    char n_str[10];
    int n = 25;

    sprintf(n_str, "%d", n);

    return 0;
}

C++:

using namespace std;
int main() {
    ostringstream n_str;
    int n = 25;

    n_str << n;

    return 0;
}
like image 44
David Titarenco Avatar answered Nov 15 '22 09:11

David Titarenco


Boost way:

string str = boost::lexical_cast<string>(n);

like image 5
dimba Avatar answered Nov 15 '22 09:11

dimba