0

I've created a program that computers the distance between two points, and finds the slope as well.

1) How would I go about changing the program to strictly pointers?

2) The distance function returns "-1" no matter what the input. I'm 100% sure my arithmetic is correct, but something seems to go wrong.

 // This program computes the distance between two points
 // and the slope of the line passing through these two points.

    #include<iostream>
    #include<iomanip>
    #include<cmath>
    using namespace std;

    struct Point
    {
        double x;
        double y;
    };

    double distance(Point &p1, Point &p2);
    double slope(Point &p1, Point &p2);

    int main()
    {
        Point p1, p2;
        char flag = 'y';
        cout << fixed << setprecision(2);
        while(flag == 'y' || flag == 'Y')
        {
            cout << "First x value: "; cin >> p1.x;
            cout << "First y value: "; cin >> p1.y;
            cout << "Second x value: "; cin >> p2.x;
            cout << "Second y value: "; cin >> p2.y; cout << endl;

            cout << "The distance between points (" << p1.x << ", " << p1.y << ") and (";
            cout << p2.x << ", " << p2.y << ") is " << distance(&p1, &p2);

            if ((p2.x - p1.x) == 0)
            { cout << " but there is no slope." << endl; cout << "(Line is vertical)" << endl; }
            else
            { cout << " and the slope is " << slope(p1, p2) << "." << endl; }

            cout << endl;
            cout << "Do you want to continue with another set of points?: "; cin>> flag;
            cout << endl;
        }
        return 0;
    }

    double distance(Point &p1, Point &p2)
    {
        return sqrt((pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2)));
    }

    double slope(Point &p1, Point &p2)
    {
        return (p2.y - p1.y) / (p2.x - p1.x);
    }
Cody
  • 37
  • 6
  • If you're going to pass the `Point`'s by reference, you'd better make them `const`...Other than that, your code looks fine. What happened when you used the debugger? – scohe001 Sep 09 '14 at 18:36
  • You seem to never allocate memory for your points `p1` and `p2`. – honk Sep 09 '14 at 18:40
  • 1
    This code obviously doesn't compile. `p1` and `p2` are uninitialized pointers, you do `p1.x` as if they're objects, and you try to pass their addresses to a function that takes objects by reference. – Blastfurnace Sep 09 '14 at 18:41
  • Please disregard the pointers I added to the initialization of p1 and p2. Without that, it compiles. But the value of the distance function always seems to be 1. @blastfurnace – Cody Sep 09 '14 at 18:44
  • I'd rather use pointers but I'm not too sure how to go about doing so. I recently started learning about them. @Josh – Cody Sep 09 '14 at 18:46
  • Your revised code does compile and run if you call distance correctly as `distance(p1, p2)`. [Live demo at ideone.com](http://ideone.com/am1ONZ) – Blastfurnace Sep 09 '14 at 18:57

1 Answers1

9

Where your code is broken:

cout << p2.x << ", " << p2.y << ") is " << distance(&p1, &p2);

You pass objects of type Point * to a function that expects Point.

Why don't we see errors from the compiler:

Because you have using namespace std, your code is probably actually calling std::distance(), and it is telling you how far apart the pointers &p1 and &p2 are.

Recommended reading:

Why is "using namespace std" considered bad practice?.

Community
  • 1
  • 1
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173