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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  #include <iostream>

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.
1
2
  const int worst_case_size = 20;
  char arrayBuscaminas[worst_case_size][worst_case_size];


2) Allocate the arrays on the heap.
1
2
  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!
1
2
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.