I'm in the mood for some easy coding, so here's the function(s) you're looking for:
Code:
#include <iostream>
/*
* itoa is the main function that's used in main
* it depends on esrever (reverse spelled backwards) to reverse the characters
* in the string it produces.
* getlen is just used to get the amount of places in a short integer
*/
void itoa(const short int in,const short int len,char*buf);
short int getlen(const short int in);
void esrever(char*in,const short int len);
int main()
{
/*
* a is the integer we're going to be turning into a string
* and word is going to be the string we put it into. NOTE: this is not
* a c-style string because there is no null-char on the end of the
* string, nor is there space for one.
*/
short int a=12345;
char*word=new char[getlen(a)];
/*
* here's the entire reason for this program: the itoa function
*/
itoa(a,getlen(a),word);
/*
* and now we just output the results, free up the allocated memory, and
* exit the program
*/
std::cout<<word<<std::endl;
delete[]word;
return 0;
}
/*
* this function returns the amount of places there are in a short int
*/
short int getlen(const short int in)
{
/*
* the method is simple: it loops through trying bigger modulo (in
* increments of powers of 10) until it gets to a point where the
* result is zero. Ever iteration of the loop increases a counter by
* one, and when the loop exits, you're left with the amount of digitsf
* in the number
*/
short int retval=0;
while(in%(10^retval)>0)
{
retval++;
}
return retval;
}
/*
* this just reverses a character array. Because we don't null-terminate our
* array, you have to pass in the size
*/
void esrever(char*in,const short int len)
{
char tmp;
register short int l;
register short int r;
/*
* it works by using a simple swap function, starting at both ends of
* the array and working it's way to the center
*/
for(l=0,r=len-1;l<r;l++,r--)
{
tmp=in[r];
in[r]=in[l];
in[l]=tmp;
}
}
/*
* this is the main focus of the program: the function that turns an integer
* into an array of characters
*/
void itoa(const short int in,const short int len,char*buf)
{
/*
* it's quite simple, actually, after you get the math part down. What
* it does is takes the number, divides it by a power of 10 to get the
* number you want in the ones place. Then, to get rid of any other
* numbers in the tens, hundreds, etc. places, you take the original
* number again and divide by one higher power of 10 and multipy by 10.
* now you have a zero in the ones place and the rest of the number in
* their places relative to the number you want. a simple subtraction
* takes care of those, and you now have a single number in the ones
* place. These divisions take advantage of the casting to integers
* that truncate the decimals. After you have that single digit, you
* simply add the integer equivalent of '0' and cast it back to a
* character before saving it in the array.
*/
for(register short int i=len-1;i>=0;i--)
{
buf[i]=static_cast<char>(((in/static_cast<int>(pow(10,i)))-((in/static_cast<int>(pow(10,(i+1))))*10))+static_cast<int>('0'));
}
/*
* that method returns a reversed string, so we now use our eserver
* method to reverse the string.
*/
esrever(buf,len);
}
now would you rather use stringstream?