itoa problem?

N

news.hku.hk

Excuse me, i write the following function to add comma for integers
but the unix server said:

In function `class string comma(int)':
implicit declaration of function `int itoa(...)'
________________________________

string comma(int a){
char to_string[50];
string s_a = itoa(a, to_string, 10);

if (a>1000000000){
s_a.insert(s_a.length()-9, ",");
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000000){
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000){
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else return (s_a);
}
_____________________________
do i use itoa wrongly or i miss anything, i've already include the header
#include <cstdlib>

Thanks
 
M

Mike Wahler

news.hku.hk said:
Excuse me, i write the following function to add comma for integers
but the unix server said:

In function `class string comma(int)':
implicit declaration of function `int itoa(...)'
________________________________

string comma(int a){
char to_string[50];
string s_a = itoa(a, to_string, 10);

if (a>1000000000){
s_a.insert(s_a.length()-9, ",");
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000000){
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000){
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else return (s_a);
}
_____________________________
do i use itoa wrongly or i miss anything, i've already include the header
#include <cstdlib>

There is no such function 'itoa()' in standard C++.
If your imlementation provides one, you'll need to
#include the header which declares it, and follow
the usage instructions given in its documentation.

Since this newsgroup only discusses standard C++,
nonstandard functions such as 'itoa()' are not
topical here.

-Mike
 
J

Julie

news.hku.hk said:
Excuse me, i write the following function to add comma for integers
but the unix server said:

In function `class string comma(int)':
implicit declaration of function `int itoa(...)'
________________________________

string comma(int a){
char to_string[50];
string s_a = itoa(a, to_string, 10);

if (a>1000000000){
s_a.insert(s_a.length()-9, ",");
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000000){
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000){
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else return (s_a);
}
_____________________________
do i use itoa wrongly or i miss anything, i've already include the header
#include <cstdlib>

Thanks

itoa() is not part of the standard C++ language. So, you will either have to
figure out the specifics of itoa on your platform/compiler, or abandon it
altogether and use something like std::eek:stringstream:

std::eek:stringstream tc;
tc << a;
string s_a = tc.str();

Aside from that, there are better ways to insert thousands separators into a
string -- if you would like other ideas, please post back.
 
M

marbac

news.hku.hk said:
do i use itoa wrongly or i miss anything, i've already include the header
#include <cstdlib>

i searched through the cstdlib (and stdlib.h) and there seems to be no
function called itoa. Maybe it is also not defined in your lib version.

regards marbac
 
N

news.hku.hk

Of course, i want better and faster method, i know my method is not good
Thanks a lot


Julie said:
news.hku.hk said:
Excuse me, i write the following function to add comma for integers
but the unix server said:

In function `class string comma(int)':
implicit declaration of function `int itoa(...)'
________________________________

string comma(int a){
char to_string[50];
string s_a = itoa(a, to_string, 10);

if (a>1000000000){
s_a.insert(s_a.length()-9, ",");
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000000){
s_a.insert(s_a.length()-6, ",");
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else if (a>1000){
s_a.insert(s_a.length()-3, ",");
return (s_a);
}
else return (s_a);
}
_____________________________
do i use itoa wrongly or i miss anything, i've already include the header
#include <cstdlib>

Thanks

itoa() is not part of the standard C++ language. So, you will either have to
figure out the specifics of itoa on your platform/compiler, or abandon it
altogether and use something like std::eek:stringstream:

std::eek:stringstream tc;
tc << a;
string s_a = tc.str();

Aside from that, there are better ways to insert thousands separators into a
string -- if you would like other ideas, please post back.
 
J

John Harrison

news.hku.hk said:
Excuse me, i write the following function to add comma for integers
but the unix server said:

In function `class string comma(int)':
implicit declaration of function `int itoa(...)'
________________________________

string comma(int a){
char to_string[50];
string s_a = itoa(a, to_string, 10);

itoa is non-standard, a simple replacement would be sprintf.

#include <stdio.h> // or <cstdio>

string comma(int a) {
char to_string[50];
sprintf(to_string, "%d", a);
string s_a = to_string;
...

john
 
S

Siemel Naran

news.hku.hk said:
Of course, i want better and faster method, i know my method is not good

See the thread "how to display 12345 as 12,345 ??" which finished a few days
ago on this newsgroup, in particular Dietmar's answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top