gpt4 book ai didi

计算区域后 C++ 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 04:12:39 26 4
gpt4 key购买 nike

我很好奇为什么我的应用程序在计算十字的面积后实际上崩溃了。

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

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

最佳答案

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

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

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

因为数组(和 vector 等)在 C++ 中是基于零的

事实上,由于 computeArea 中的循环,您可以看到这是预期的范围。

程序仅在计算后崩溃的原因是越界处理(特别是写入)调用了未定义的行为:它可能会崩溃或执行随机操作:这不是错误检测。


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

但是,您最好(重新)设计您的类,使其仅承担 1 项责任(即:代表一个十字,不进行输入或输出。此外,临时变量 xValyVal 不应超出输入过程(并且可以合并)。

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

关于计算区域后 C++ 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com