Help please.

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

int main() {
    
    // +++Set Board+++
    char again;
    do {
        system("cls");
        int row=10, col=10; // Default Area of Board
        char setup;
        cout << "Do you want to setup the area of the game?" << endl;
        cin >> setup;
        if (tolower(setup)=='y') {
            system("cls");
            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+++
            if(move=='w') {
                board[prow][pcol]='O';
                prow--;
                if(prow<0) {
                    cout << "Invalid Move.";
                    getch();
                    prow++;
                }
                board[prow][pcol]='+';
            }
            if(move=='s') {
                board[prow][pcol]='O';
                prow++;
                if(prow>row-1) {
                    cout << "Invalid Move.";
                    getch();
                    prow--;
                }
                    board[prow][pcol]='+';
            }
            if(move=='a') {
                board[prow][pcol]='O';
                pcol--;
                if(pcol<0) {
                    cout << "Invalid Move.";
                    getch();
                    pcol++;
                }
                    board[prow][pcol]='+';
            }
            if(move=='d') {
                board[prow][pcol]='O';
                pcol++;
                if(pcol>col-1) {
                    cout << "Invalid Move.";
                    getch();
                    pcol--;
                }
                    board[prow][pcol]='+';
            }
            
            // +++Check Trap+++
            int i;
            for (i=0; i<trapcount-1; 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]);
        do {
            cout << "Would you like to play again?" << endl;
            cin >> again;
        } while (tolower(again)!='y' && tolower(again)!='n');
    } while(tolower(again)=='y');
    return 0;
}


*edit1: updated my code. still has errors.
*edit2: updated my code. solved. thanks to Peter87.
Last edited on
Line 22: Variable length arrays are not supported by standard C++.

Your compiler makes some sort of guessing and makes the array of length 10,10 since that is what you initialize the row and col variables with. Weird compiler. In any case, what you're doing is not supported by C++, (it's supported by C99 though).

Try valarray: http://www.cplusplus.com/reference/std/valarray/.
@unoriginal, I don't get it. what should I do then? :|
What unoriginal talking about is not an error. It's just that you are using an extension that is not standard C++.
@Peter87, any alternatives that will function the same but will not commit runtime error? :|
@unoriginal, when I initialize the row and col by 5 I still get runtime error when I input 5 row, 5 col. but works when 10 row, 10 col. why?
You can use std::vector instead but as I said it's not what causing the error.

Don't call srand more than once. Each time you call srand with the same seed you will get the same sequence of numbers. Call it once at the start of main.

Can you explain a bit more when the error happen and what happens?
@Petery87, okay let me edit that.
Hmm. error occurs when the area of the game is not 10,10. tried 5,5; 15,15; 10,15; 20,60; i get runtime errors during the first move. :|
I found an error on line 134, i can be equal to trapcount which is out of bounds for trprow and trpcol.
Last edited on
@Peter87, also, if I remove the board[prow][pcol]='+'; the runtime error doesn't occur but ofcourse it would not function correctly.

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
// +++Moves+++
            if(move=='w') {
                board[prow][pcol]='O';
                prow--;
                if(prow<0) {
                    cout << "Invalid Move.";
                    getch();
                    prow++;
                }
                board[prow][pcol]='+'; // this
            }
            if(move=='s') {
                board[prow][pcol]='O';
                prow++;
                if(prow>row-1) {
                    cout << "Invalid Move.";
                    getch();
                    prow--;
                }
                    board[prow][pcol]='+'; // this
            }
            if(move=='a') {
                board[prow][pcol]='O';
                pcol--;
                if(pcol<0) {
                    cout << "Invalid Move.";
                    getch();
                    pcol++;
                }
                    board[prow][pcol]='+'; // this
            }
            if(move=='d') {
                board[prow][pcol]='O';
                pcol++;
                if(pcol>col-1) {
                    cout << "Invalid Move.";
                    getch();
                    pcol--;
                }
                    board[prow][pcol]='+'; // this
            }
Last edited on
@Peter87, Wow didn't notice that. THANKS ALOT! :D it is now working. i'll post here if there are some more issues. thanks again!
Topic archived. No new replies allowed.