简体   繁体   English

C ++编程…卡在if..else if ...条件下

[英]C++ programming … stuck on if..else if… conditions

I created this little software which can calculate the surface and the perimeter of the rectangle and the square. 我创建了这个小软件,可以计算矩形和正方形的表面和周长。 However, it is getting very hard for me to run this program. 但是,对于我来说,运行该程序变得非常困难。 What I mean is that when I try to run it, the software works fine for the "square" part, but it closes immediately when I go to the "rectangle" part. 我的意思是,当我尝试运行该软件时,该软件对于“正方形”部分运行良好,但是当我进入“矩形”部分时,该软件立即关闭。 Can anyone help me? 谁能帮我? Thank you very much! 非常感谢你!

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

You are missing a closing brace before else if (x==2) else if (x==2)您将错过一个右括号

It is a good idea to make the indentation match the braces - easier to spot this type of bug ` 使缩进与花括号匹配是一个好主意-更容易发现这种类型的错误`

You have a curly bracket(}) missplaced. 您放错了花括号(})。 You need to close it before else if (x==2) because otherwise the else if applies to else if (y==2) instead to x == 1. Improve your code style as pointed in IntermediateHacker's comment and you should be able to avoid such errors. 您需要在else if (x==2)之前将其关闭,因为否则else else if适用于else if (y==2)而不是x ==1。按照IntermediateHacker的注释中所述改进代码样式,您应该能够避免此类错误。

I have spend a lot of time formatting your code. 我花了很多时间来格式化您的代码。 Still there are obvious problems with it. 仍然有明显的问题。 The curly braces just do not match. 花括号不匹配。 You don't even have opening brace for the main method. 您甚至没有主方法的开括号。 You also do not have curly brace before else if (x==2) . else if (x==2)也没有花括号。 If you have paid some more effort formatting your code it would have been easier for you to detect your problem. 如果您付出了更多的努力来格式化代码,那么您将更容易发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM