Re: placing int into char*
Ok so let me clarify..............
sudo code:
Code:
int x = 10;
char* buf = new char[ number of digits ];
take x place into buf like '1' and '0'
so that...
buf[0] = 1
buf[1] = 0
Re: placing int into char*
I just typed this up on the fly, so there's probably some errors:
Code:
int intPow(const int n, const int p) // Gets n to the power of p
{
int ret = n;
for(int i = 1; i < p; i++)
ret *= n;
return ret;
}
int intLog10(const int n) // Gets log base 10
{
int ret = 0, num = n;
while(num > 10)
{
num /= 10;
ret++;
}
return ret;
}
char* intToStr (const int n) // Call this one with the number and it (maybe) will return it in string form
{
int strLen = intLog10 + 2, num = n, temp, temp2;
char* ret = new char[strLen];
for(int i = 0; i < strLen-1; i++)
{
char[i] = (temp2 = (num % (temp = intPow(10,strLen-i-2)))) + '0';
num -= temp * temp2;
}
ret[strLen-1] = '\0';
return ret;
}
There's also itoa, of course:
http://www.cplusplus.com/reference/c.../cstdlib/itoa/
Re: placing int into char*
very nice i will try it and get back her when i figure out what happens
Re: placing int into char*
Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648. So if you pass a fixed-sized char array with at least 12 bytes to itoa, you know you'll have enough space.
Re: placing int into char*
just out of curiosity what is the #include for iota...... and isnt it a C# function
correct me if im wrong
Re: placing int into char*
Quote:
Originally Posted by Lindley
Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648.
Though strictly speaking an int could have an even larger range on a given implementation, but such a consideration is not so important for a school assignment.
Quote:
Originally Posted by ductiletoaster
just out of curiosity what is the #include for iota...... and isnt it a C# function
correct me if im wrong
It is not part of the C++ standard library, but if it is available you can probably #include <cstdlib> to use it.