Compiler Errors !!!!!!!!!!!!!

I am trying to run this source code but i am getting the compiler error Expression Must Have a Constant Value. I tried making both the variables x and y constants and assigned them to a significantly big number but then i am getting the error expression must be a modifiable lvalue.I have made comments in the code in front where Visual Studio is giving me the syntax error (red squiggly line), any 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include<iostream>
#include <string>
#include<cmath>
using namespace std;



int main(){

    int x;              
    int y;
    int error = 0;     
    int xtemp = 0;      
    int ytemp = 0;
    char a;             
    char temp[99];      


    fgets(temp, 99, stdin);
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    y = atoi(temp);//RIGHT HERE the y has a squiggly underline. When i make both the varibales x & y constants with the error message "expression must be a modifiable lvalue."
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    fgets(temp, 99, stdin);
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    x = atoi(temp);//RIGHT HERE the x has a squiggly underline.When i make both the varibales x & y constants with the error message "expression must be a modifiable lvalue."
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    char maze[y][x]; //RIGHT HERE the x and y has squiggly underlines with the error message "Expression must have a constant value"
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    for (; ytemp < y; ytemp++)
        fgets(maze[ytemp], x + 2, stdin);



    int iX = -1; 
    int iY = -1;
    int fX = -1; 
    int fY = -1;


    for (ytemp = 0; ytemp < y; ytemp++){
        for (xtemp = 0; xtemp < x; xtemp++){
            a = maze[ytemp][xtemp];
            if (a == 'L' && iX == -1){      
                iX = xtemp;                 
                iY = ytemp;
            }
            else if (a == 'L' && iX != -1){ 
                error = 1;
            }

            if (a == 'X' && fX == -1){       
                fX = xtemp;                 
                fY = ytemp;
            }
            else if (a == 'X' && fX != -1){ 
                error = 1;
            }


            if (a != 'X' && a != 'L' && a != '.' && a != '#'){
                error = 1;
            }
        }
    }

    if (iX == -1){
        error = 1;
    }

    if (fX == -1){
        error = 1;
    }

    if (error == 1){
        fprintf(stderr, "invalid input\n");
    }


    int xP = iX;       
    int yP = iY;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int flood[y][x];//RIGHT HERE the x and y has squiggly underlines with the error message "Expression must have a constant value"
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    for (ytemp = 0; ytemp < y; ytemp++){
        for (xtemp = 0; xtemp < x; xtemp++){
            flood[ytemp][xtemp] = -1;
        }
    }


    flood[iY][iX] = 0;


    int found = 0;
    int moves = 0;
    int itt = 1;        
    int moremoves = 1;


    while (found != 1 && moves < 150 && error == 0 && moremoves == 1){
        moremoves = 0;      


        for (ytemp = 0; ytemp < y; ytemp++){
            for (xtemp = 0; xtemp < x; xtemp++){
                xP = xtemp;
                yP = ytemp;


                if (flood[ytemp][xtemp] == (itt - 1)){


                    if (
                        ((char)maze[yP - 1][xP] == '.' ||
                        (char)maze[yP - 1][xP] == 'L' ||
                        (char)maze[yP - 1][xP] == 'X')
                        && (yP - 1) >= 0
                        && flood[yP - 1][xP] == -1
                        ){
                        flood[yP - 1][xP] = itt;  
                        moremoves = 1;


                        if ((char)maze[yP - 1][xP] == 'X'){
                            flood[yP - 1][xP] = itt;
                            found = 1;
                        }
                    }


                    if (
                        ((char)maze[yP + 1][xP] == '.' ||
                        (char)maze[yP + 1][xP] == 'L' ||
                        (char)maze[yP + 1][xP] == 'X')
                        && (yP + 1) < y
                        && flood[yP + 1][xP] == -1
                        ){
                        flood[yP + 1][xP] = itt;
                        moremoves = 1;
                        if ((char)maze[yP + 1][xP] == 'X'){
                            flood[yP + 1][xP] = itt;
                            found = 1;
                        }
                    }


                    if (
                        ((char)maze[yP][xP - 1] == '.' ||
                        (char)maze[yP][xP - 1] == 'L' ||
                        (char)maze[yP][xP - 1] == 'X')
                        && (xP - 1) >= 0
                        && flood[yP][xP - 1] == -1
                        ){
                        flood[yP][xP - 1] = itt;
                        moremoves = 1;
                        if ((char)maze[yP][xP - 1] == 'X'){
                            flood[yP][xP - 1] = itt;
                            found = 1;
                        }
                    }


                    if (
                        ((char)maze[yP][xP + 1] == '.' ||
                        (char)maze[yP][xP + 1] == 'L' ||
                        (char)maze[yP][xP + 1] == 'X')
                        && (xP + 1) < x
                        && flood[yP][xP + 1] == -1
                        ){
                        flood[yP][xP + 1] = itt;
                        moremoves = 1;
                        if ((char)maze[yP][xP + 1] == 'X'){
                            flood[yP][xP + 1] = itt;
                            found = 1;
                        }
                    }

                }



            }
        }

        itt++;
        moves++;


    }

    int complete = 0;
    int directions[150] = {};
    int i = 0;


    if (found == 0 && error == 0){
        printf("no path\n");
    }
    else if (error == 0){
        xP = fX;
        yP = fY;

        while (complete == 0){


            if (flood[yP - 1][xP] == flood[yP][xP] - 1 && (yP - 1) >= 0
                ){
                yP--;
                directions[i] = 3;  
                i++;
                if (flood[yP][xP] == 0){
                    complete = 1;
                }
            }
            else if (flood[yP + 1][xP] == flood[yP][xP] - 1 && (yP + 1) < y
                ){
                yP++;
                directions[i] = 1;  
                i++;
                if (flood[yP][xP] == 0){
                    complete = 1;
                }
            }
            else if (flood[yP][xP - 1] == flood[yP][xP] - 1 && (xP - 1) >= 0
                ){
                xP--;
                directions[i] = 2;  
                i++;
                if (flood[yP][xP] == 0){
                    complete = 1;
                }
            }
            else if (flood[yP][xP + 1] == flood[yP][xP] - 1 && (xP + 1) < x
                ){
                xP++;
                directions[i] = 4;  
                i++;
                if (flood[yP][xP] == 0){
                    complete = 1;
                }
            }

        }

    }


    if (error == 0){

        for (; i >= 0; i--){
            if (directions[i] == 1){
                printf("North\n");
            }
            else if (directions[i] == 2){
                printf("East\n");
            }
            else if (directions[i] == 3){
                printf("South\n");
            }
            else if (directions[i] == 4){
                printf("West\n");
            }



        }
    }


    if (error == 1){
        return 1;
    }
    else{
        return 0;
    }
}
atoi takes a char and you are passing a string. however, if you are going to be using c++ i suggest std::string and std::cin as opposed to char[] and fgets(..., stdin)
int flood[y][x]

declaring array sizes requires constants, the number of elements must be calculable at compile time.
In line 31, you are trying to allocate memory statically, or on the stack. Lines 10 - 16 do the same thing. In each of these earlier cases, the compiler can allocate memory at compile time on the program stack to handle the variables requested. In lines 10 - 14, 4 bytes (probably) are set aside for these variables. In line 15 1 byte is set asside, and in line 16 99 bytes are set aside. When the compiler gets to line 31, however, it does not know what the values of x and y are (because the program hasn't been run yet), so it has no idea how much memory to set aside for this 2-dimensional array.

The answer is to allocate memory on the heap, or dynamically. See the following tutorial page.

http://www.cplusplus.com/doc/tutorial/dynamic/

I tried making both the variables x and y constants and assigned them to a significantly big number but then i am getting the error expression must be a modifiable lvalue.

Another alternative that you apparently tried to do is to declare a "big enough" array statically and then use part of it. The problem you ran into is that you tried to store new values in the constant "variables" used to initialize the array. The constant values you use to initialize the array must be different from the variables you use to read in the user's sizes. What you could have done is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    const int maxRows = 1000;
    const int maxCols = 1000;
    int x;              
    int y;

    ...

    char maze[maxRows][maxCols];

    fgets(temp, 99, stdin);
    y = atoi(temp);
    // Verify that y < maxRows

    fgets(temp, 99, stdin);
    x = atoi(temp);
    // Verify that x < maxCols

    ...
Topic archived. No new replies allowed.