I am doing a minesweeper program for school, but i keep getting this error on my code
cannot allocate an array of constant size 0
I do not know why this is happening; I am not allocating the size — I am setting that on 0. Another problem is, how can I read my input char by char, so I can save it in my array?
As you can see below, I am using an input and output. I comented my input and my ouput so you guys can see what im using for this program. I want to read char by char so I can save all the map on the array.
I am using MSVC++2010.
freopen("input.txt","rt",stdin);
//4 4
//*...
//....
//.*..
//....
//3 5
//**...
//.....
//.*...
//0 0
freopen("output.txt","wt",stdout);
/*Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100*/
int n=-1;
int m=-1;
int cont =0;
while(n!=0 && m!=0)
{
scanf("%d %d",&n,&m);
int VMatriz[n][m]={0};
int Mapa[n][m]={0};
if (n==0 && m==0)
break;
cont++;
printf("Field #%d",cont);
for (int i=0;i<n;i++)
{ printf("/n");
for (int j=0;j<m;j++)
{
scanf("%d ",&Mapa[i][j]);
if (Mapa[i][j]=='*')
{
if (j-1>=0)
VMatriz[i][j-1]++;
if (j+1<m)
VMatriz[i][j+1]++;
if (i-1>=0)
VMatriz[i-1][j]++;
if (i+1<m)
VMatriz[i+1][j]++;
if (j-1>=0 && i-1>0)
VMatriz[i-1][j-1]++;
if (j-1>=0 && i+1<m)
VMatriz[i+1][j-1]++;
if (j+1<m && i-1>0)
VMatriz[i-1][j+1]++;
if (j+1<m && i+1<m)
VMatriz[i+1][j+1]++;
VMatriz[i][j]='*';
printf("%d",VMatriz[i][j]);
}
}
}
printf("/n");
}
return 0;
}
This is illegal. As is this simpler version;
However…
The error you actually care about here is this one, not the “cannot allocate an array of constant size 0” error.
You cannot allocate an array of unknown size with automatic storage duration in C++. If you want a variable sized array then you need to dynamically allocate it (or, better yet; just use a
vector).Note that there is a gcc extension to allow this, but not in VS (and it is not standard C++. It was submitted for C++ 11, but ultimately declined.)