我使用嵌套的結構/結構,經過幾個小時的僞代碼和嘗試,我提出的最終結果不起作用或不能編譯。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'一個錯字? –
比較浮點值時要非常小心 - 小錯誤通常會蔓延到計算中,導致結果不盡如人意。 –