Question

When I populate the array(doska) all is ok, but when I try to print element(cout<< I get error

   #include <iostream>
    using namespace std;
    struct doskas{
         int number;
        char ch;
    };
    int main(){
        auto doska= new doskas[8][8];
        auto ss="0abcdefgh";
        for(int i=1;i<=8;i++){
            for(int j=1;j<=8;j++){
            doska[i][j].ch=ss[i];
            doska[i][j].number=j;
            }
        }

        for(int i=1;i<=8;i++)
            for(int j=1;j<=8;j++){
                cout<<doska[i][j].ch;//ERROR
                cout<<doska[i][j].number;
            }

        system("pause");
        return 0;
    }
Was it helpful?

Solution

Try from 0 and strictly less than 8, not from one to eight.

OTHER TIPS

Array indices must always start with 0 and end with N-1 where N is the size of the array. Please change your index variables in all the for loops accordingly. Like this:

for(int i=0;i<8;i++)
{
  //etc
}

You just have to enumerate array indices in the half-open range [0, N), and all is well:

  for (int i = 0; i < 8; ++i)
  {
    for (int j = 0; j < 8; ++j)
    {
      doska[i][j].ch = ss[i];
      doska[i][j].number = j;
    }
  }

See Dijkstra's famous argument on why this is the sanest way to think about ranges.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top