How do I store "46123" into an array that holds up to 30 digits? with the numbers going into the array like this:
0 1 2 ...... 25 26 27 28 29 (array spots)
[] [] [] ... [4] [6] [1] [2] [3]
If your number is a string, you could make use of the STL std::string functions like length and at to calculate where to put each char. Make sure you check that the string doesn't have extraneous spaces or any other undesirables, try one of the find functions. Use a for loop to do it with.
Well since you are starting from the back to front makes it very simple.
Since the numbers are in decimal base we can use modulus 10 - % 10 to get the rightmost digit. Then after we add it to the end of the array we can remove it from the number by dividing by 10 - / 10
So it will look something like:
1 2 3 4 5 6 7 8 9 10 11
void AddToArray( int SomeNumber30DigitsOrLess , int Array[] )
{
int Offset = 29; //array size - 1
while( SomeNumber30DigitsOrLess ) //loop while it is true (not 0)
{
Array[Offset] = SomeNumber30DigitsOrLess % 10;
SomeNumber30DigitsOrLess /= 10;
--Offset; //Forgot to add this earlier
}
}
*edit I probably should not have assumed you had an integer as the others mentioned if it is a string to start with it'd be a lot simpler :P