array filling

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]

int fabulousArray[30]
fabulousArray[26] = 4;
fabulousArray[27] = 6
fabulousArray[28] = 1;
fabulousArray[29] = 2;
fabulousArray[30] = 3;


THAT'S ALL WRONG
Last edited on
the numbers are random I can't just set a spot in the array equal to it like that
does the number get generated as "46123" (using this number as example) or are the numbers generated as individual digits
@heyyouyesyouiloveyou

fabulousArray[30] is out of bounds for the array.

@jwilt

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.

Hope all goes well.
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

*fixed code
Last edited on
@TheIdeasMan

oh yeah, oops :P
the input file has strings of numbers but I think I can make it work with the length and at functions
Topic archived. No new replies allowed.