1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

confused on why the point is not working

Discussion in 'C++' started by ZohebN, Apr 8, 2008.

  1. ZohebN

    ZohebN New Member

    Joined:
    Apr 8, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Alright so I have this program that I am working on that has the following instructions:
    Code:
    Write a C++ class called point. You should create the following 3 separate files:
    •	Point.h: the header file for the point class
    •	Point.cpp: the code and data for the point class; and
    •	PointDriver.cpp: a program to call and test your point class
    The PointDriver.cpp program should ask the user for the name of a data file. This data file will contain the coordinates for two points, one coordinate per line, in the order x, y, z. Your program should then declare two point objects. One point should be initialized by passing arguments for x, y, and x. The second should be initialized using the separate functions for setting the value of x, y, and z. For these two points, the distance between the two points and the midpoint should be computed and displayed. The attributes for one point should also be displayed using the display function. For the other point, the attributes should be displayed using the individual accessor functions.
    All data elements should be doubles and displayed with 3 decimal places.
    
    the output should look like this:
    Code:
    Enter filename: 
    Midpoint Coordinates: 3.500, 4.500, 5.500
    The distance is 12.450
    Point Coordinates: 1.000, 1.000, 1.000
    Point Coordinates: 6.000, 8.000, 10.000
    
    but right now mine looks like this
    Code:
    Enter Filename: 
    Midpoint coordinates: 1.000, 1.000, 1.000
    The distance is: 0.000
    Point A: 1.000, 1.000, 1.000
    Point B: 1.000, 1.000, 1.000
    
    i dont know what is wrong with it because i have followed the instructions i was given, but don't know how the second point ends up with the values it is suppose to have.

    my code is as follows:
    point.h
    Code:
    #ifndef POINT_H_
    #define POINT_H_
    
    class Point
    {
    public:
        void setx(double);
        void sety(double);
        void setz(double);
        double getx();
        double gety();
        double getz();
        double getDistance(Point&);
        Point midpoint(Point&);
        bool displayPoint();
        Point();
        Point(double, double, double);
        ~Point();
    
    private:
        double x;
        double y;
        double z;
    };
    #endif
    
    point.cpp
    Code:
    #include "Point.h"
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    Point::Point() {
        x = 0;
        y = 0;
        z = 0;
    }
    Point::Point(double pX, double pY, double pZ) {
        x = pX;
        y = pY;
        z = pZ;
    }
    Point::~Point() {
    
    }
    void Point::setx(double pX) {
        x = pX;
    }
    void Point::sety(double pY) {
        y = pY;
    }
    void Point::setz(double pZ) {
        z = pZ;
    }
    double Point::getx() {
        return x;
    }
    double Point::gety() {
        return y;
    }
    double Point::getz() {
        return z;
    }
    double Point::getDistance(Point& otherPoint) {
        double oX = otherPoint.getx();
        double oY = otherPoint.gety();
        double oZ = otherPoint.getz();
        double dX,dY,dZ;
        dX = pow((x - oX),2);
        dY = pow((y - oY),2);
        dZ = pow((z - oZ),2);
        return sqrt((dX + dY + dZ));
    }
    Point Point::midpoint(Point& otherPoint) {
        double oX = otherPoint.getx();
        double oY = otherPoint.gety();
        double oZ = otherPoint.getz();
        double mX,mY,mZ;
        mX = ((getx() + oX) / 2.0);
        mY = ((gety() + oY) / 2.0);
        mZ = ((getz() + oZ) / 2.0);
        Point returnPoint;
        returnPoint.setx(mX);
        returnPoint.sety(mY);
        returnPoint.setz(mZ);
        return returnPoint;
    }
    
    bool Point::displayPoint() {
        std::cout << x << ", " << y << ", " << z << endl;
        return 1;
    }
    
    driverpoint.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <iomanip>
    #include "Point.h"
    
    using namespace std;
    
    int main ()
    {
        char filename[100];
        double x,y,z;
        ifstream inFile;
       
        cout << "Enter Filename: ";
        cin >> filename;
        inFile.open(filename);
        inFile >> x >> y >> z;
    
        Point a(x,y,x);
        Point b(x,y,z);
       
       
        Point m;
        cout << fixed << showpoint << setprecision(3);
        m = a.midpoint(b);
        cout << "Midpoint coordinates: " << m.getx() << ", " << m.gety() << ", " << m.getz() << endl;
    
        cout << "The distance is: " << a.getDistance(b) << endl;
        cout << "Point A: "; a.displayPoint();
    
        cout << "Point B: " << b.getx() << ", " << b.gety() << ", " << b.getz() << endl;
    	inFile.close();
        return 0;
    }
    
     

Share This Page