0

I am -trying- to use nested structs/structures, and after several hours of pseudocode and attempts, the final result that I come up with doesn't work or doesn't compile.

I would like to take two vectors A and B, and compare them against each other. I set up nested struct to read the start and end point of the vector, and the vector struct itself. So I think I may be doing some wrong further below, but I am stuck.

    #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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

5 Answers5

2

One problem is with your ReadPoint whose return type is Point, but you're returning an instance of MathVector. Also, you read the input into variables which ignore eventually.

You should write ReadPoint as:

Point ReadPoint()
{
    Point p;
    cout << "Please input the x-coordinate: " << endl;
    cin >> p.x;
    cout << "Please input the y-coordinate: " << endl;
    cin >> p.y;
    cout << "Please input the z-coordinate: " << endl;
    cin >> p.z;
    return p;
}

Or a little better version:

Point ReadPoint()
{
    Point p;
    cout << "Please enter point-coordinate : " << endl;
    cin >> p.x >> p.y >> p.z; //example input : 1 2 3 
    return p;
}

Or, still better is, overload >> operator as:

std::istream & operator>>(std::istream & in, Point & p)
{
    cout << "Please enter point-coordinate : " << endl;
    return cin >> p.x >> p.y >> p.z; //example input : 1 2 3 
}

//Use this as
Point pointA, pointB;
cin >> pointA >> pointB;

Now read a good C++ book. If you're already reading one, then make sure it is really good. Here is a list of really good C++ books, of all levels:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • thanks Nawaz.... for now I am only learning C++ structs to skip ahead of my high school class :) If I take some programming in course uni or college, I will definately take a look – alberto roberto Nov 25 '11 at 05:48
  • thank for your help! one more thing though and i am on my way... letterA = ReadPoint(); letterB = ReadPoint(); is saying "no match for 'operator=' in 'letterB = ReadPoint()'" WHAT DOES THIS MEAN? – alberto roberto Nov 25 '11 at 05:52
  • You're trying to set a vector equal to a point, but you've never specified what exactly that should do. So the computer has no idea. – David Schwartz Nov 25 '11 at 06:32
0
  1. ReadPoint returns letter of type MathVector instead of Point
  2. You haven't overloaded operator << to tell it how to handle MathVector objects
adrianton3
  • 2,258
  • 3
  • 20
  • 33
0
Point ReadPoint()
{
   MathVector letter; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
    double x, y, z;

   cout << "Please input the x-coordinate: " << endl;
   cin >> x;
   cout << "Please input the y-coordinate: " << endl;
   cin >> y;
   cout << "Please input the z-coordinate: " << endl;
   cin >> z;

   return letter;

}

You didn't explain what it is you're trying to do or what errors you got, but this code makes no sense. You have three variables, x, y, and z. You fill them with values you get from the user. Then you don't do anything with those variables and return the MathVector created by a default constructor even though you say you're going to return a Point. That makes very little sense.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I created a vector, Point, that should handle three dimensions, x y z. Then created a struct, MathVector, so that the START of a vector and the END of the vector could use the struct Point. Two of these MathVectors are created as 'A' and 'B' so that two seperate vectors can be put into a calculation. – alberto roberto Nov 25 '11 at 05:38
  • 1
    Okay, so you need to read *4* points and load them into the `start` and `end` positions of the two vectors. You never wrote any code to do that. – David Schwartz Nov 25 '11 at 08:07
0

letterA and letterB are of type MathVector

 MathVector letterA;
        MathVector letterB;
        double a_times_b;

        letterA = ReadPoint();
        letterB = ReadPoint();

you should create another method to read Mathvector.. as you are doing with Point.

and in method ReadPoint

return type must be Point .. If you reading point then do calculation here to create the object of MathVector go tet startpoint and endpoint format.

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

No match for 'operator=' error means that there's no function for assigning a MathVector to a Point. You are calling ReadPoint() which returns a Point and trying to assign the returned value to a variable of type MathVector. The compiler can't create a 'convertion' function automatically. You have to provide one yourself. Perhaps what you meant was

letterA.start = ReadPoint();
letterA.end   = ReadPoint();
frozenkoi
  • 3,228
  • 22
  • 33