我只是好奇为什么我的应用程序在计算十字区域后实际上崩溃了。
我的输出是正确的,但计算完成后它会崩溃。
交叉.cpp
void Cross::setCrossCord()
{
for (int i=1; i<=12; i++)
{
cout << "Please enter x-ordinate of pt " << i << ": ";
cin >> xVal;
xvalue[i] = xVal;
cout << endl;
cout << "Please enter y-ordinate of pt " << i << ": ";
cin >> yVal;
yvalue[i] = yVal;
cout << endl;
}
}
double Cross::computeArea()
{
int points = 12;
int running_total = 0;
for (int i=0; i<=12-1; i++)
{
running_total = (xvalue[i]*yvalue[i+1]) - (xvalue[i+1]*yvalue[i]); //cross calculation of coord in a cross
} //(x1*y2)-(y1*x1)
running_total = (xvalue[points-1]*yvalue[0]) - (xvalue[0]*yvalue[points-1]); // traverse back to the origin point
// (xn*y1)-(yn*x1)
area = abs(running_total / 2); //purpose of absolute is to make sure result is positive.
//polygon are specified in counter-clockwise order (i.e. by the right-hand rule), then the area will be positive.
cout << "area of cross is: " << area << endl;
return (area);
}
int main()
{
Cross cross;
string shapetype;
cout << "enter shape type: " << endl;
cin >> shapetype;
if(shapetype == "cross")
{
cross.setCrossCord();
}else
{cout << "error" << endl;};
cross.computeArea();
}
这是我从 Windows 得到的错误,我很困惑为什么会这样。