Help realy needed

This is a discussion on Help realy needed within the C++ Programming forums, part of the General Programming Boards category; Alright so I have this program that I am working on that has the following instructions: Write a C++ class ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Help realy needed

    Alright so I have this program that I am working on that has the following instructions:
    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;
    }
    Last edited by CornedBee; 04-08-2008 at 03:29 AM. Reason: Preserve forum layout.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Perhaps

    Code:
        inFile >> x >> y >> z;
        Point a(x,y,x);
        inFile >> x >> y >> z;
        Point b(x,y,z);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    And:
    Code:
    Point a(x,y,z);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 10:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 12:55 PM
  3. Battleship Game (Win32 EXP Needed)
    By ElWhapo in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 01-15-2005, 10:10 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-01-2004, 12:24 AM
  5. Using pointers - asterisks needed ?
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-28-2002, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21