C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-07-2008, 10:10 PM   #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:
Quote:
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 02:29 AM. Reason: Preserve forum layout.
ZohebN is offline   Reply With Quote
Old 04-07-2008, 11:21 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,676
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.

Salem is offline   Reply With Quote
Old 04-08-2008, 09:37 AM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,251
And:
Code:
Point a(x,y,z);
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
free needed or not? quantt Linux Programming 3 06-25-2009 09:32 AM
C Programmers needed for Direct Hire positions canefan Projects and Job Recruitment 0 09-24-2008 11:55 AM
Battleship Game (Win32 EXP Needed) ElWhapo Projects and Job Recruitment 2 01-15-2005 10:10 PM
C++ help needed Enkindu Projects and Job Recruitment 3 08-31-2004 11:24 PM
Using pointers - asterisks needed ? Nutshell C Programming 5 01-28-2002 06:56 PM


All times are GMT -6. The time now is 01:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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