1. itoa trouble

I don't have an itoa() function for my compiler. So i decided to write one. The code folloes, but it has an odd bug. The func works perfectly until it exits the for loop, and it somehow "forgets" everything it has stored in my num array. Please help!

Code:
```char *itoa(int i, char num[256]){
int rem = 0;
int digits = 1;
int x=i;

for (digits; true; digits++){
if (x<10){
cout << "Digits: " << digits << endl;
break;
} else {
rem = x % 10;
x = (x-rem)/10;
//cout << n << ": i:" << i << endl;
}
}
rem=0;
for (int n=digits; n>=0; n--){
if (i<10){
num[n]=(char)(i+48);
cout << n << ": n:" << num[n] << " :" << (int)num[n] << endl;
cout << "    " << num[n] << endl;
break;
} else {
rem = i % 10;
num[n]=(char)(rem+48);
cout << n << ": n:" << num[n] << " :" << (int)num[n] << endl;
cout << n << ": R:" << rem << endl;
i = (i-rem)/10;
cout << n << ": i:" << i << endl;
}
cout << "    " << num[n] << endl;

}
num[digits]='\0';
cout << "Number: " << (int)num[0] << endl;
if(getche());
return num;
}```
All help will be appreciated.
~Inquirer

2. Try and change char num[256] into char* num. You need a pointer, otherwise num will go out of scope when exiting the function and you can't access the data anymore.

3. use sprintf ("%d", var)

4. OT

Seems like it's very popular with those "anti coding style" avatars .

5. never knew that! thanks!

and the couts are just for debugging... so i can't use printf()

~Inquirer

6. Originally posted by Inquirer
never knew that! thanks!

and the couts are just for debugging... so i can't use printf()

~Inquirer
not like that sentence made sense (cout goes to the same place as printf), but i said use sprintf not printf . and if you're using cout why not just

int i;
...
cout << i;

??

7. The couts are just for debuging, and to prove to me that the loop was doing what it was supposed to. Using sprintf() wouldn't (i don't think ) help me convert a number into a string to bre sent through a socket. What is sorintf anyway? String print f?

~Inquirer

8. sprintf() won't print to the screen, unlike printf() (notice the s in front). Instead it will print to a buffer (char array) so you can do whatever you want with the string.

9. How would I implement that?

And does anyone know why the array goes out of scope, or ow to circumvent that? Ir will sprintf not make it go out of scope?

~Inquirer

10. >Using sprintf() wouldn't (i don't think ) help me convert a number into a string to bre sent through a socket.
The end result is the same as the non-standard itoa, a string with the numeric value of an integer.

>What is sorintf anyway?
You don't know what it is yet you say that it won't work for your purposes?
Code:
```#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

const int DIGITS = 10;

char *itoa ( int n )
{
int sign;

if ( ( sign = n ) < 0 )
n = -n;

int i = 0;
char *s = new char[DIGITS];

do
s[i++] = static_cast<char> ( n % 10 + '0' );
while ( ( n /= 10 ) > 0 );

if ( sign < 0 )
s[i++] = '-';
s[i] = '\0';

std::reverse ( s, s + std::strlen ( s ) );

return s;
}

int main()
{
int i = 12345;

char *p = itoa ( i );
std::cout<< p <<std::endl;
delete p;

char a[10];
std::sprintf ( a, "%d", i );
std::cout<< a <<std::endl;

std::cin.get();
}```
-Prelude

11. Aparently, my idea of what sprintf was is wrong... thanks Prelude. I'm going to go step through that code to try to fugure out what it does, too. Thanks all.

~Inquirer