我正在尝试运行此源代码,但我收到编译器错误Expression Must Have a Constant Value
。我尝试同时制作变量x
和y
常量并将它们分配给一个非常大的数字,但后来我得到错误expression must be a modifiable lvalue
。我几乎被困在这里不能让程序编译,我使用Visual Studio 2013作为我的IDE。我在前面的代码中做了注释,Visual Studio给我的语法错误(红色波浪线),任何帮助!!!!!!
我检查了相同编译器错误的所有其他帖子无法找到二维数组的问题。
#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;
}
}