### Compiling error in rare minesweeper cases (Help)

So this is basically what I have to input and output. It is sort of a minesweeper function where it counts the number of mines around each slot.

Sample Input

2 (number of cases)
3
5
**...
.....
.*...
2
2
.*
..

Sample Output

Case 1:
**100
33200
1*100

Case 2:
1*
11

I have already made the code and it works most of the times. One problem that I have is when I input a board full of mines, it throws an error like this:
input:

****
****
****
****

output:

3***
****
****
****

Here is the code:
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 #include using namespace std; char input(istream& in=cin){ char x; in >> x; return x; } int main() { int m; int n; int c; int contador= 0; cout << "Introduce the number of cases:" << endl; cin >> c; if (c >=0 && c <=20) { for(int i=0; i<=c ;i++) { cin >> m; cin >> n; char arrayBuscaminas[m][n]; if(m>1 && n<10) { contador++; for(int j=0; j < m; j++) { for(int k=0; k < n; k++) { arrayBuscaminas[j][k]= input(); } } } else{ break; } int arrayNumeros[m][n]; for(int l=0; l < m; l++) { for(int t=0; t < n; t++) { arrayNumeros[l][t]=0; } } for(int x=0; x < m; x++) { for(int y=0; y < n; y++) { if(arrayBuscaminas[x][y]== '*') { arrayNumeros [x - 1] [y - 1]++; arrayNumeros [x - 1] [y]++; arrayNumeros [x - 1] [y + 1]++; arrayNumeros [x] [x - 1]++; arrayNumeros [x] [x + 1]++; arrayNumeros [x + 1] [y - 1]++; arrayNumeros [x + 1] [y]++; arrayNumeros [x + 1] [y + 1]++; } } } cout << "Caso" << contador<<":"<< endl; for(int w=0; w < m; w++) { for(int v=0; v < n; v++) { if(arrayBuscaminas[w][v]== '*'){ cout << '*'; } else cout << arrayNumeros[w][v]; } cout << endl; } cout << endl; } } }

Hope someone can help me out. Thanks in advance.
Line 29 - m and n are not const expressions. Some compilers allow non-const expressions, but c++ standard does not.

Line 45 - same issue.

Thank you for replying. I know I can change the char array to be const char, but how do I fix the line 45 issue? Sorry for being dumb. I'm fairly new to programming.

Would that help with the errors I am getting?
Changing line 29 from chat to const char is not the issue.

The issues is that the values of m and n are not known at compile time, therefore the compiler doesn't know how to allocate the array.

You have a couple of options.

1) allocate the arrays at lines 29 and 45 as worst case.
 12 const int worst_case_size = 20; char arrayBuscaminas[worst_case_size][worst_case_size];

2) Allocate the arrays on the heap.
 12 char * arrayBuscaminas; arrayBuscaminas = new char[m][n];

Be sure and delete the array at the bottom of the loop, or you will have a memory leak.

Looks to me like you need to do some bounds checking for "arrayNumeros".

e.g. line 59: arrayNumeros [x - 1] [y - 1]++;

What happens here when x==0 && y==0?
@AbstractionAnon Thank you, I made the changes allocating the arrays as worst case and it works fine now. I also had a silly error in the lines 62 & 63 where it was supposed to be Y instead of X, that was creating mistakes in the final results. It works perfectly now.

Thanks for the help!
 12 char * arrayBuscaminas; arrayBuscaminas = new char[m][n];

This doesn't work. Did you mean new char[m*n];?
Last edited on
Yes, I meant new char[m*n];

Topic archived. No new replies allowed.