struct boardType
{
    int boardNumber[8][7][7];
    int numberOfBoards;
};

//note: int main() is assumed as standard "bloodshed DEV-C++" compiler 

boardType totalBoards;
totalBoards.numberOfBoards = 8;
totalBoards.boardNumber[0] = {{0,0,0,0,0,0,0},
                              {6,6,6,1,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {5,5,5,5,5,5,5}};

expected primary-expression before '{' token
expected ';' before '{' token

Above shows the script I am trying to make work. the quote is the error that is built when first row compiles. Im trying to see where but my ignorance and in ability to memorize array syntax is truely showing

EDIT: what I am trying to do is store 8 boards that are 7x7 each. I don't wish to use vectors because I am trying to understand arrays. Vectors come later :P

3 dimentional arrays or a one dimentional struct that stores a 7x7 array. Im using structs to store the full list of arrays.

struct boardType
{
    int boardNumber[7][7];
};
     boardType currentBoard[8];
    currentBoard[0].boardNumber = {{0,0,0,0,0,0,0},
                              {6,6,6,1,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {5,5,5,5,5,5,5}};

Dani AI

Generated

A quick, practical follow-up that ties together the approaches already shown by and and gives concrete ways to persist and reuse those 7x7 boards at runtime.

If you want an external file (easy to edit and inspect) use a simple text layout and read/write with nested loops. The pattern below writes each number separated by spaces and reads them back in the same order. It is portable and easy to debug.

bool save_text(const int boards[8][7][7], const char *fn) {
    std::ofstream out(fn);
    if (!out) return false;
    for (int b = 0; b < 8; ++b)
        for (int r = 0; r < 7; ++r) {
            for (int c = 0; c < 7; ++c)
                out << boards[b][r][c] << (c+1==7?'\n':' ');
            out << '\n';
        }
    return true;
}

bool load_text(int boards[8][7][7], const char *fn) {
    std::ifstream in(fn);
    if (!in) return false;
    for (int b = 0; b < 8; ++b)
        for (int r = 0; r < 7; ++r)
            for (int c = 0; c < 7; ++c)
                if (!(in >> boards[b][r][c])) return false;
    return true;
}

If you prefer compact/fast storage you can write the raw bytes in binary with a single write/read of the whole block. That is fast but nonportable across different architectures and compiler settings (endianness, sizeof(int), padding), so use it only when you control both writer and reader.

To copy boards in memory at runtime you have three simple choices: element-wise loops, a single memcpy (fast, safe when the memory layout is plain ints), or modern container assignment if you switch to std::array (which supports operator=). Example with memcpy:

std::memcpy(destBoard, srcBoard, sizeof destBoard); // destBoard and srcBoard are int[7][7]

Final notes: keeping large initializers in a separate source file or marking them const is tidy (what you did with hangmanBoard). If learning, prefer text files and explicit loops — they teach the mechanics. When you move to newer compilers, consider std::array or a small class wrapper for clearer semantics and automatic copying. For reference on std::array behavior see cppreference (std::array).

Recommended Answers

All 4 Replies

You can only initialise arrays, you can't assign them on-mass like you're trying to do.

boardType totalBoards = {
                              {
                              {0,0,0,0,0,0,0},
                              {6,6,6,1,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {6,6,6,6,6,6,1},
                              {5,5,5,5,5,5,5}
                              },
                              8
};

o.0 oh I see :( that's too bad

so, what can I do? I know the values of each element of all the boards, and I want to store them to a '.dat' file so that they can be loaded when the app runs. Is a strucure not a good way to go? Or should I just script them in and not worry about reading from a file (really it will be a small app, probably no more than 1000 lines)I guess I could initialize all 8 boards at once and just store them as different names I guess.
what about something like this?

boardType currentBoard[8] = {
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,1,3,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,6,6,2,6,1},{5,5,5,5,5,5,5}},
       {{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,3,6,2,6,1},{5,5,5,5,5,5,5}}
       };

any advice here?

I tried to give it one more edit but no luck..
so anywho,

I temporarily solved the problem by initializing a const array of

const int hangmanBoard[8][7][7] = {
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,6,1,3,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,6,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,6,6,6,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,6,6,2,6,1},{5,5,5,5,5,5,5}},
{{0,0,0,0,0,0,0},{6,6,6,1,6,6,1},{6,6,6,4,6,6,1},{6,6,2,1,3,6,1},{6,6,6,1,6,6,1},{6,6,3,6,2,6,1},{5,5,5,5,5,5,5}}
};

and will use pieces of it as I need it. The array does it's purpose, but still would like to write it to a file and read it from a file in the end.

problem is not truely solved to my specifications unless someone could help guide my noobyness in the right direction with proper code design and array/object manipulation
(each board is a numeric representation of a graphic that will be printed on the screen) -- hangman boards :P

Sure, there's nothing to stop you using some loops, to read a bunch of values out of a file, and assign to your arrays one element at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.