From: marcas on
hirenshah.05(a)gmail.com schrieb:
> i trids itoa, but it is not compiling. so please tell me how to
> convert
> integer to string
>

itoa is a not defined within ansi-c.
Beside the solution posted before, you could implement
your own c-style itoa:


#include <iostream>
#include <string>


using namespace std;

//Declaration of itoa
char *itoa (int);

int main () {
int myint=-123456;
string mystring (itoa(myint));
cout << mystring << endl;
}


// Implementation of itoa
// source:
// http://www.uclibc.org/lists/uclibc/2000-December/000081.html
// not verified

//*************SNIP***************************************
#include <limits.h>

#if INT_MAX > 2147483647
#error need to increase size of buffer
#endif

/* 10 digits + 1 sign + 1 trailing nul */
static char buf[12];

char *itoa(int i)
{
char *pos = buf + sizeof(buf) - 1;
unsigned int u;
int negative = 0;

if (i < 0) {
negative = 1;
u = ((unsigned int)(-(1+i))) + 1;
} else {
u = i;
}

*pos = 0;

do {
*--pos = '0' + (u % 10);
u /= 10;
} while (u);

if (negative) {
*--pos = '-';
}

return pos;
}

//*************SNIP***************************************

First  |  Prev  | 
Pages: 1 2
Next: What is panic?