Where I can find itoa()?

  • Thread starter silverburgh.meryl
  • Start date
S

silverburgh.meryl

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 function 'int main()':
.../t.cpp:13: error: 'itoa' was not declared in this scope


/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}

Thank for any help.
 
A

Alf P. Steinbach

* (e-mail address removed):
Can you please tell me where I can find itoa()?

That depends on the compiler, since it's not a standard C++ library
function (although it's not uncommon).

In standard C++ you can generally use a std::eek:stringstream to convert
from number to string, but std::eek:stringstream only supports three
radixes: decimal (by std::dec), octal (by std::eek:ct) and hex (std::hex).

If you want binary, as in your example code, or other radixes not
directly supported, you can easily code a conversion function yourself
(or for binary, if you're just interested in the bits, you can use a
std::bitset to do the conversion).
 
F

fiNAL.Y

itoa() is not a standard function of C.
Among the compilers I've used, only MSVC support this function.
lexical_cast of Boost is a better choice for conversion.
 
A

Alf P. Steinbach

* fiNAL.Y:
itoa() is not a standard function of C.
Among the compilers I've used, only MSVC support this function.
lexical_cast of Boost is a better choice for conversion.

Please quote what you're responding to.

See the FAQ and the monthly welcome-posting in this group.

Thank you.
 
J

Jack Klein

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

Add a newline at the end of the file. It is not hard. Move your
cursor to just past the last character of the last line in the file
and press the enter key, then save the file. C++ requires that any
source of header file ends with a newline.
../t.cpp: In function 'int main()':
../t.cpp:13: error: 'itoa' was not declared in this scope


/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}

Thank for any help.

Write your own itoa(), it's not difficult. Or use sprintf() instead.
It is completely standard, and is present on every conforming C and
C++ implementation on the planet.
 
P

Phlip

Jack said:
Add a newline at the end of the file. It is not hard. Move your
cursor to just past the last character of the last line in the file
and press the enter key, then save the file. C++ requires that any
source of header file ends with a newline.

I thought that was just GNU c++.
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top