我使用嵌套的结构/结构,经过几个小时的伪代码和尝试,我提出的最终结果不起作用或不能编译。C++结构不编译...未正确初始化?不正确使用它们?
我想采取两个向量A和B,并将它们相互比较。我设置了嵌套结构来读取矢量的开始和结束点以及矢量结构本身。所以我想我可能会在下面做一些错误,但我被卡住了。
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct Point // Reads in three coordinates for point to make a three dimensional vector
{
double x;
double y;
double z;
};
struct MathVector // Struct for the start and end point of each vector.
{
Point start;
Point end;
};
Point ReadPoint()
{
Point pt; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
double x, y, z;
cout << "Please input the x-coordinate: " << endl;
cin >> pt.x;
cout << "Please input the y-coordinate: " << endl;
cin >> pt.y;
cout << "Please input the z-coordinate: " << endl;
cin >> pt.z;
return pt;
}
void DotProduct (MathVector letterA, MathVector letterB, double& a_times_b) //formula to compute orthogonality
{
a_times_b = (letterA.end.x - letterA.start.x)*(letterB.end.x - letterB.start.x) + (letterA.end.y - letterA.start.y)*(letterB.end.y - letterB.start.y) + (letterA.end.z - letterA.start.z)*(letterB.end.z - letterB.start.z);
}
int main()
{
MathVector letterA;
MathVector letterB;
double a_times_b;
letterA = ReadPoint();
letterB = ReadPoint();
DotProduct (letterA, letterB, a_times_b);
cout << "The vector " << letterA << " compared with " << letterB << " ";
if (a_times_b == 0)
cout << "is orthoganal." << endl;
else
cout << "is not orthoganal." << endl;
return 0;
}
发布错误。另外,“不工作”和“不编译”是两回事。 – Nawaz
事实上'ReadPoint()'不会更新'letter'一个错字? –
比较浮点值时要非常小心 - 小错误通常会蔓延到计算中,导致结果不尽如人意。 –