Need help! Newbie here.

I encounter a runtime error during the first move.
But when I set the board to 10 row 10 col I don't get a runtime error.
Please help.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

int main() {
    
    // +++Set Board+++
    int row=10, col=10;
    do {
        cout << "Enter the number of rows(1-20): ";
        cin >> row;
    } while (row>20 || row<1);
    do {
        cout << "Enter the number of columns(1-60): ";
        cin >> col;
    } while (col>60 || col<1);
    
    cout << "Loading Map....." << endl;
    cout << "Player is +" << endl;
    cout << "Treasure is ?" << endl;
    char board[row][col];
    for (int i=0; i<row; i++) {
        for (int j=0; j<col; j++) {
            board[i][j]='O';
        }
    }
    int trapcount=(row+col)/2;
    int trprow[trapcount], trpcol[trapcount], prow, pcol, trow, tcol;
    char player='+', treasure='?', traps[trapcount];
    for (int i=0; i<trapcount; i++) {
        traps[i]=i+75;
        do {
            srand(time(NULL));
            trprow[i]=rand()%row;
            trpcol[i]=rand()%col;
            if (board[trprow[i]][trpcol[i]]=='O') {
                board[trprow[i]][trpcol[i]]=traps[i];
            }
        } while(board[trprow[i]][trpcol[i]]!=traps[i]);
    }
    do {
        srand(time(NULL));
        prow=rand()%row;
        pcol=rand()%col;
        if (board[prow][pcol]=='O') {
            board[prow][pcol]='+';
        }
    } while (board[prow][pcol]!='+');
    do {
        srand(time(NULL));
        trow=rand()%row;
        tcol=rand()%col;
        if (board[trow][tcol]=='O') {
            board[trow][tcol]='?';
        }
    } while (board[trow][tcol]!='?');
    
    // +++Game+++
    do {
        system("cls");
        for (int i=0; i<row; i++) {
            int j=0;
            for (j=0; j<col-1; j++) {
                cout << board[i][j];
            }
            cout << board[i][j] << endl;
        }
    
        char move;
        do {
            cout << "w-UP; s-DOWN; a-LEFT; d-RIGHT" << endl;
            cin >> move;
        } while(move!='a' && move!='d' && move!='s' && move!='w');
        
        // +++Moves+++ // I am suspecting the problem occurs here. :|
        if(move=='w') {
            board[prow][pcol]='O';
            prow--;
            if(prow<0) {
                cout << "Invalid Move.";
                prow++;
            }
            board[prow][pcol]='+';
        }
        if(move=='s') {
            board[prow][pcol]='O';
            prow++;
            if(prow>row) {
                cout << "Invalid Move.";
                prow--;
            }
                board[prow][pcol]='+';
        }
        if(move=='a') {
            board[prow][pcol]='O';
            pcol--;
            if(pcol<0) {
                cout << "Invalid Move.";
                pcol++;
            }
                board[prow][pcol]='+';
        }
        if(move=='d') {
            board[prow][pcol]='O';
            pcol++;
            if(pcol>col) {
                cout << "Invalid Move.";
                pcol--;
            }
                board[prow][pcol]='+';
        }
        
        // +++Check Trap+++
        int i;
        for (i=0; i<trapcount; i++) {
            if (board[prow][pcol]==board[trprow[i]][trpcol[i]]) {
                system("cls");
                cout << "You found the trap!" << endl;
                cout << "BOOM!! YOU LOSE!" << endl;
                break;
            }
        }
        if (board[prow][pcol]==board[trprow[i]][trpcol[i]])
            break;
        // +++Check Win+++
        if (board[trow][tcol]==board[prow][pcol]) {
            system("cls");
            cout << "You found the treasure!" << endl;
            cout << "You win! Congratulations!" << endl;
            break;
        }
    } while(board[trow][tcol]!=board[prow][pcol]);
        
    
    getch();
    return 0;
}
You should declare what your program is supposed to do. What is the problem and the exact error messages.

From seeing your code I can say that it's natural to have errors since you are expecting to define the array dimension at runtime! This should be know during compile time. So not only that but need to be const also. Anyway I am not sure if your complier requires that but you cannot expect to define array dimensions during runtime.

Also you use <conio.h> which is ancient history. How old is your compiler anyway?

And some final point: you also use platform dependent, unsecure not recommended commands that are not part of c++:
system("cls");
should be avoided.

Hope I helped
Change in the part of the code .

1
2
3
4
5
6
7
8
9
10
11
12
13
 // +++Game+++
    while (true )
     {
        system("cls");
        for (int i=0; i<row; i++) {
            int j=0;
            for (j=0; j<col-1; j++) {
                cout << board[i][j];
            }
            cout << board[i][j] << endl;
			if( i == row )
				break; 
        }

Topic archived. No new replies allowed.