C++ 错误:输入结束时应为“}”——当有一个时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13355300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:13:39  来源:igfitidea点击:

error: expected ‘}’ at end of input -- when there is one

c++compiler-errors

提问by user1757575

I am getting this error when compiling:

编译时出现此错误:

project6.cpp: In function ‘int main()':

project6.cpp:在函数“int main()”中:

project6.cpp:187: error: expected ‘}' at end of input

project6.cpp:187: 错误:输入结束时应为“}”

However, there is clearly an end bracket to my int main () function at that line, so I am confused as to why I am getting this error. I also checked all of my other brackets and found none that were not closed. Any help would be much appreciated!

但是,在那行我的 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

回答by jogojapan

The first elsestatement in the big for-loop misses a closing brace.

else大 for 循环中的第一条语句遗漏了一个右大括号。

回答by Aamir

One of your else blocks does not have a closing brace... Look below:

您的 else 块之一没有右大括号...请看下面:

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

Due to this, the total number of closing braces is not equal to the opening braces, hence the error.

因此,右大括号的总数不等于左大括号,因此出现错误。

回答by 0605002

The error is here:

错误在这里:

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

The elseblock has not been closed.

else块尚未关闭。

回答by Yogendra Singh

Add a closing brace in the section below(at marked line):

在下面的部分中添加一个右大括号(在标记行):

  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
  }

回答by selbie

You are missing a closing curly bracket } in the else clause below:

您在下面的 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;
  }

Should be:

应该:

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

回答by Works On Mine

I think this block is missing a proper ending bracket,

我认为这个块缺少一个正确的结束括号,

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