简体   繁体   中英

C++ array and window exit

I'm new in C++ but I know well C#, java. But no matter. I want to create checkers game emulation. I edded new event for window - load. This is source of what I've did:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             int matr[7][7];
             int i, j;
             int prevBlack = 1;
             int prevRow = 0;
             int current = 0;

             for (i = 0; i < 8; i++)
             {
                 if ((i + 1) % 2 == 0)
                 {
                     prevBlack = 0;
                 }
                 else
                 {
                     prevBlack = 1;
                 }

                 for (j = 0; j < 8; j++)
                 {
                     if (prevBlack == 1)
                     {
                         current = 0;
                     }
                     else if (i == 0 || i == 1 || i == 2)
                     {
                          current = 2;
                     }
                     else
                     {
                         current = 1;
                     }

                     matr[i][j] = (int)current;

                     if (current == 1 || current == 2)
                     {
                         prevBlack = 1;
                     }
                     else
                     {
                        prevBlack = 0;
                     }
                 }

                 prevRow = i;
             }
         }

The problem is matr[i][j] = (int)current; At the end of this part of code my program exits. When this part of code is commented window will be displayed.

I don't know why it is so. array is 8 x 8. I need your help :)

When you declare your array as matr[7][7] , it will have the size 7x7, and valid indices will be 0..6. But you are accessing this array with indices 0..7, which results in an error.

You should declare the array as matr[8][8] since you need a 8x8 array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM