473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

itoa problem?

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
Jul 22 '05 #1
7 12726

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.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>


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
Jul 22 '05 #2
"news.hku.hk" wrote:

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::ostringstream:

std::ostringstream 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.
Jul 22 '05 #3
news.hku.hk schrieb:
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
Jul 22 '05 #4
Of course, i want better and faster method, i know my method is not good
Thanks a lot
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
"news.hku.hk" wrote:

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::ostringstream:

std::ostringstream 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.

Jul 22 '05 #5

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.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);


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
Jul 22 '05 #6
"news.hku.hk" wrote:

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


Look into facets and numpunct.
Jul 22 '05 #7
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
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.

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
4
by: Moritz Beller | last post by:
Hello! Is there an equivalent to Visual C++'s itoa function in gcc? best regards Moritz Beller -- web http://www.4momo.de mail momo dot beller at t-online dot de...
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
2
by: Sona | last post by:
Hi, I have a char* that holds an ascii character in its first element (at least I think that's what it holds because when I print it, it prints weird characters). I need to convert this into an...
29
by: pete | last post by:
I wrote a version of itoa yesterday. Features: 1 No implementation defined arithmetic. All of the division and modulus division is done on positive values only. 2 No object is assumed...
11
by: rayw | last post by:
I'm pretty new to C, although I did do some years ago now. I've been told that itoa is no longer a standard function, and that the ato... functions - although in the std - are not recommended. ...
24
by: Mark | last post by:
hi, all i want is a simple function that takes an int, and returns a char* so i tried char * itoa(int i) { char buff; return _itoa(i,buff,10); }
7
by: silverburgh.meryl | last post by:
Hi, Can you please tell me where I can find itoa()? I try to compile the following example, but I get the following error: .../t.cpp:20:2: warning: no newline at end of file .../t.cpp: In...
6
by: khairiabdulrahim | last post by:
this is my code: #include <stdio.h> #include <stdlib.h> int main () { int j; char str;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.