编译时出现此错误:
project6.cpp:在函数'int main()'中:
project6.cpp:187:错误:输入结束时预期的“}”
但是,我的 int main () 函数在该行显然有一个结束括号,所以我对为什么会收到此错误感到困惑。我还检查了所有其他括号,发现没有一个没有关闭。任何帮助将非常感激!
#include <iostream>
using namespace std;
void initRace(char grid[52][72])
{
for(int r = 0; r < 52; r = r + 1)
{ for(int c = 0; c < 72; c = c + 1)
{ grid[r][0] = 'X';
grid[0][c] = 'X';
grid[r][71] = 'X'; // border
grid[51][c] = 'X';
}
for(int c = 65; c <= 70; c = c + 1)
{ grid[51][c] = 'F'; // finish line
}
}
for(int r = 1; r <= 35; r = r + 1)
{ for(int c = 10; c <= 29; c = c + 1)
{ grid[r][c] = 'X'; // first barrier
}
}
for(int r = 16; r <= 50; r = r + 1)
{ for(int c = 40; c <=64; c = c + 1)
{ grid[r][c] = 'X'; //second barrier
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =1; c <=9; c = c + 1)
{ grid[r][c] = ' '; //first block of spaces
}
}
for(int r = 36; r <= 50; r = r + 1)
{ for(int c =10; c <=29; c = c + 1)
{ grid[r][c] = ' '; //second block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =30; c <=39; c = c + 1)
{ grid[r][c] = ' '; //third block of spaces
}
}
for(int r = 1; r <= 15; r = r + 1)
{ for(int c =40; c <=64; c = c + 1)
{ grid[r][c] = ' '; //fourth block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =65; c <=70; c = c + 1)
{ grid[r][c] = ' '; //fifth block of spaces
}
}
grid[1][1] = 'O';
}
void printRace(char grid[52][72])
{ for (int i = 0 ; i < 52; i = i + 1)
{ for (int j = 0 ; j < 72; j = j + 1)
{ cout << grid[i][j] << " ";
}
cout << endl;
}
}
int main(void)
{ char grid[52][72];
initRace(grid);
int xAcceleration;
int yAcceleration;
int xVelocity = 0;
int yVelocity = 0;
int xPosition = 1;
int yPosition = 1;
for(int i = 1; i < 100; i = i + 1)
{ printRace(grid);
cout << "Horizontal and vertical acceleration (-1,0,1): ";
cin >> xAcceleration;
cin >> yAcceleration;
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
if((yAcceleration != 0) && (yAcceleration != 1) && (yAcceleration != -1))
{ printRace(grid);
if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
}
i = 500;
}
xVelocity = xVelocity + xAcceleration;
yVelocity = yVelocity + yAcceleration;
xPosition = xPosition + xVelocity;
yPosition = yPosition + yVelocity;
if((xPosition >= 10) && (xPosition <=29) && (yPosition >= 1) && (yPosition<= 35))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into first barrier
i = 500;
}
if((xPosition >= 40) && (xPosition <= 64) && (yPosition >= 16) && (yPosition <= 50))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into second barrier
i = 500;
}
if(xPosition <= 0) //crashed into left border
{ grid[yPosition][0] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(yPosition <= 0) //crashed into top border
{ grid[0][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(xPosition >= 71) //crashed into right border
{ grid[yPosition][71] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((yPosition >= 51) && (xPosition >= 1) && (xPosition <= 39)) //crashed into bottom border
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((xPosition >= 65) && (xPosition <= 70) && (yPosition >= 51)) // crossed finish line
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Finished after " << i << " seconds" << endl;
i = 500;
}
grid[yPosition][xPosition] = 'O';
}
return 0;
} // THIS IS LINE 187