我创建了这个小软件,可以计算矩形和正方形的表面和周长。但是,我很难运行这个程序。我的意思是,当我尝试运行它时,该软件适用于" square"部分,但当我进入"矩形"它会立即关闭。部分。谁能帮我?非常感谢你!
#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;
}
答案 0 :(得分:6)
您在else if (x==2)
最好让缩进与大括号相匹配 - 更容易发现这种类型的错误 `
答案 1 :(得分:0)
你错放了一个大括号(})。您需要在else if (x==2)
之前关闭它,否则其他地方如果适用于else if (y==2)
而不是x == 1.改进您的代码风格,如IntermediateHacker评论所指出的那样,您应该能够避免这样的错误。
答案 2 :(得分:0)
我花了很多时间格式化你的代码。它仍有明显的问题。花括号只是不匹配。你甚至没有主方法的开括号。你在else if (x==2)
之前也没有大括号。如果您付出了更多努力来格式化代码,那么您将更容易发现问题。