1

我只是好奇为什么我的应用程序在计算十字区域后实际上崩溃了。

我的输出是正确的,但计算完成后它会崩溃。

交叉.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 得到的错误,我很困惑为什么会这样。 错误的打印屏幕

4

3 回答 3

2

您必须将 for 循环更改为setCrossCord从零开始:

// change
for (int i=1; i<=12; i++)

// to
for (int i=0; i<12; i++)

因为数组(和向量等)在 C++ 中是从零开始的

事实上,您可以看到这是预期的范围,因为computeArea.

程序仅在计算后崩溃的原因是因为越界处理(特别是写入)调用了Undefined Behavior:它可能会崩溃或做随机的事情:它不是错误检测。


这是您代码的“技术上”固定版本:Live on Coliru

但是,您最好(重新)设计您的类以仅承担一项责任(即:代表一个十字架,不做输入或输出。此外,临时xVal人员yVal不应超出输入程序(并且可以合并) )。

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

struct Cross
{
    double xvalue[12], yvalue[12];

    void setCrossCord()
    {
        for (int i=0; i<12; i++)
        {
            cout << "Please enter x-ordinate of pt " << i << ": "; 
            double xVal, yVal;
            cin >> xVal;
            xvalue[i] = xVal;
            cout << endl;
            cout << "Please enter y-ordinate of pt " << i << ": ";
            cin >> yVal;
            yvalue[i] = yVal;
            cout << endl;
        }
    }

    double 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)

        double 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.

        return (area);
    }

};
int main()
{
    Cross cross;
    string shapetype;

    cout << "enter shape type: " << endl;
    cin >> shapetype;

    if(shapetype == "cross")
    {
        cross.setCrossCord();
    }else
    {
        cout << "error" << endl;
    };

    cout << "area of cross is: " << cross.computeArea() << endl;
}
于 2013-11-02T16:08:40.490 回答
0

我敢打赌您的索引是错误的 - C/C++ 使用基于 0 的索引,因此:

对于 (int i=1; i<=12; i++)

for (int i=0; i<=12-1; i++)

是腥的。此外 index + 1 是。

于 2013-11-02T16:08:30.990 回答
0

1) 你的代码
cin>>yval;
yvalue[i]=yval;
可以改为
cin>>yvalue[i];
2)数组从下标0开始而不是1
3)在主函数中返回一些东西(在main()的末尾添加return 0;语句) 4)为什么要从computearea()返回任何值,让它保持无效并且不返回任何事情,因为您在函数本身中使用了该值。
(我认为第 3 点)会导致错误,但要纠正代码中的所有四个点。

于 2013-11-02T16:26:42.943 回答