C++编程......如果...条件,就会卡在if..else上

0 c++ if-statement

我创建了这个小软件,可以计算矩形和方形的表面和周长.但是,我很难运行这个程序.我的意思是,当我尝试运行它时,软件适用于"方形"部分,但当我转到"矩形"部分时它会立即关闭.谁能帮我?非常感谢你!

#include<iostream>
using namespace std;
int main ()
{
double a, b, c, answer, x, y, z;
cout << "Square (1) or rectangle (2) ";
cin >> x ;
if (x==1)
{
    cout << "Square side:  ";
    cin >> a;
    cout << " Type (2) if you would like to calculate the Perimeter or (1) if you would like to calculate the surface?";
    cin >> y;
    if (y == 1)
    {
        cout<<"The surface of the square is: ";
        answer = ( a * a );
        cout << answer << endl;
    }
    else if (y == 2)
    { 
        cout << "The perimeter of the square is: ";
        answer = (4*a);
        cout << answer << endl;
     }     
else if (x==2)
{
     cout << "The first side of the rectangle is: ";
     cin >> c;
     cout << "The second side of the rectangle is: ";
     cin >> b;
     cout << " Type (2) if you would like to calculate the Perimeter or (1) if you would like to calculate the surface? ";
     cin >> z;
     if (z == 1)
     {
         cout << "The surface of the rectangle is: ";
         answer = (c*b);
         cout << answer<<endl;
      }
      else if (z == 2)
      { 
          cout << "The perimeter of the rectangle is: ";
          answer = 2 * (c + b);
          cout << answer << endl;
       }
   }
   system("pause");
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ed *_*eal 6

你以前错过了一个右大括号 else if (x==2)

使缩进与大括号相匹配是一个好主意 - 更容易发现这种类型的错误