Array not assigning correctly.

I need to implement some code into my program that changes the values of a 9 integer array, I tried using the following code but on my console screen it shows boxes and some random stuff, (so its not working for some reason.)
1
2
3
4
                for (int iX = 0; iX <9; ++iX)
                {
                    caBoard[iX] = (iX+1);
                }


I need to assigned caBoard with the numbers 1-9,
Well, this code is perfectly fine. Must be something else wrong, please provide the full code.
on my console screen it shows boxes and some random stuff,


How are you displaying it?
It could be that you are not creating the array correctly, I'm only assuming because you say you are getting random characters.
You might be doing this?
1
2
int size_of_array = 9;
caBoard[size_of_array];

If so either just put in 9 into caBoard right off the start OR make size_of_array constexpr.
My psychic powers predict that caBoard[] is an array of characters, not integers.

1
2
3
4
5
6
7
8
    char caBoard[10] = {0};   // make length one more than required, to allow null terminator
     
    for (char ix = '1'; ix <= '9'; ++ix)
    {
        caBoard[ix - '1'] = ix;
    }    
    
    cout << caBoard << '\n';


or replace lines 3 to 6 with
1
2
3
4
    for (int ix = 0; ix < 9; ++ix)
    {
        caBoard[ix] = ix + '1';
    }
Last edited on
or replace lines 3 to 6 with
1
2
3
4
    for (int ix = 0; ix < 9; ++ix)
    {
        caBoard[ix] = ix + '1';
    }

I recommend this solution because if you get something wrong it's better that you get the char value wrong than the array indexing.
Yes! It is an array of characters! Damn I forgot about that. Thanks guys, I got it working!
Topic archived. No new replies allowed.