4

コンパイル時に次のエラーが発生します。

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
4

6 に答える 6

3

大きな for ループの最初のelseステートメントで右中括弧が欠落しています。

于 2012-11-13T03:46:43.053 に答える
3

あなたのelseブロックの1つに閉じ括弧がありません...以下を見てください:

     else
     {  cout << "Crashed after " << i << " seconds" << endl;
     printRace(grid);
     i = 500;

このため、右中括弧の総数が左中括弧と等しくないため、エラーが発生します。

于 2012-11-13T03:47:55.503 に答える
1

エラーは次のとおりです。

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;
  }

elseブロックは閉じられていません。

于 2012-11-13T03:48:25.400 に答える
1

以下のelse句に閉じ中括弧}がありません:

  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((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;
     }
  }
于 2012-11-13T03:50:27.053 に答える
0

以下のセクション (マークされた行) に右中括弧を追加します。

  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;
      }//<-- This was missing
  }
于 2012-11-13T03:48:41.770 に答える
0

このブロックには適切な終了ブラケットがないと思いますが、

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;
  }
于 2012-11-13T03:50:34.683 に答える