I have the following code

This is the Point header file

``````#ifndef POINT_H
#define POINT_H

#include<string>
using namespace std;

//Class Point represents points in the Cartesian coordinate

class Point{
protected:
double xCoord, yCoord;           //coordinates of the point
string name;                     //name of the point

public:
Point();                         //default constructor
Point(double, double, string);    //conversion constructor

double getX() const;
double getY() const;

string getName() const;

void setX(double);
void setY(double);

void setName(string);

double distanceFrom(Point&);
void printP();
};

#endif``````

This is the cpp file defining the Point class:

``````#include <iostream>
#include <string>
#include <cmath>
#include "Point.h"
#include "Point3D.h"
using namespace std;

Point::Point() {    //default constructor
xCoord = 0;
yCoord = 0;
name = "origin";
}

Point::Point(double x, double y, string str) {    //conversion constructor
xCoord = x;
yCoord = y;
name = str;
}

//accessor methods
double Point::getX() const {
return xCoord;
}

double Point::getY() const {
return yCoord;
}

string Point::getName() const {
return name;
}

void Point::setX(double x) {
xCoord = x;
}

void Point::setY(double y) {
yCoord = y;
}

void Point::setName(string str) {
name = str;
}

ostream& operator<<(ostream& o, const Point3D& p)
{
return o;
}``````

This is the Point3D header file:

``````#ifndef POINT3D_H
#define POINT3D_H

#include<string>
using namespace std;

//Class Point represents points in the Cartesian coordinate

class Point3D: public Point{
private:
double zCoord;           //coordinates of the point

public:
Point3D();                         //default constructor
Point3D(double, double, double, string);    //conversion constructor

double getZ() const;
void setZ(double);

double distanceFrom(Point3D&);
string getName() const;
void printP();

};

#endif``````

This is the cpp file defining the Point3D class:

``````#include <iostream>
#include <string>
#include <cmath>
#include "Point.h"
#include "Point3D.h"
using namespace std;

Point3D::Point3D() {    //default constructor
zCoord = 0;
setX(0);
setY(0);

}

Point3D::Point3D(double x, double y, double z, string str) {    //conversion constructor
setX(x);
setY(y);
zCoord = z;
name =str;
}

//accessor methods
double Point3D::getZ() const {
return zCoord;
}

void Point3D::setZ(double z) {
zCoord = z;
}

ostream& operator<<(ostream& o, const Point3D& d)
{
return o;
}

double Point3D::distanceFrom(Point3D& p) {    //distance between this point & p
return  sqrt((xCoord - p.xCoord) * (xCoord - p.xCoord) +
(yCoord - p.yCoord) * (yCoord - p.yCoord) +
(zCoord - p.zCoord) * (zCoord - p.zCoord));
}
string Point3D::getName() const {
return name;
}
void Point3D:PrintP() {
cout << name << " (" << xCoord << ", " << yCoord << ", " << zCoord << ")" << endl;
}``````

And this is the cpp file to test if it works:

``````#include <iostream>
#include <string>
#include "Point.h"
#include "Point3D.h"
#include <fstream>

using namespace std;

// function prototypes
void copy(double dist, fstream& outFile);

int main(int argc, char* argv[])
{
fstream inf;
fstream outf1;
fstream outf2;

if (argc != 3)
{
cerr << "Usage: " << argv << " infile outfile";
cerr << "\nCopy contents of infile to outfile" << endl;
exit(1);
}

inf.open(argv, ios::in);

// Check if the input file exists or not
if (!inf)
{
cerr << "File " << argv << " does not exist.  Halting program!" << endl;
exit(1);
}

// open the output file in read mode first to see if the file exists or not
outf1.open(argv, ios::in);

if (outf1)
{
char ans;

cerr << "\nFile " << argv << " already exists!  Overwrite? (Y/N) --> ";
cin >> ans;
if ((ans != 'Y') && (ans != 'y'))
exit(1);                     // don't overwrite
}

// outfile does not exist or allows overwriting
outf1.close();

// now open the output file in output mode.
// If the file already exists, it will be overwritten;
// otherwise it will be created
outf2.open(argv, ios::out);
if (!outf2)
{
cerr << "\nCan not open output file." << endl;
exit(1);
}

Point3D p0;
Point3D p1(8, 5, 5, "A");
Point3D p2(3, 6, 10, "B");

p0.printP();
p1.printP();
p2.printP();

double dist = p1.distanceFrom(p2);

copy(dist, outf2);

inf.close();
outf2.close();

system ("pause");
return 0;
}

void copy(double dist, fstream& outFile)
{ //copy the contents of inFile to outFile
outFile << dist;
}``````

I know that this bunch of files, but I am not allowed to combine them, since this is a test of my knowledge of inheritance.

I get the following error:
error C2451: conditional expression of type 'std::fstream' is illegal

``````if (outf1)
{
char ans;``````

After I figure out what is wrong, I will have to finish the code. The program will have to take values from an input file, which will contain two or more points, and the program should take two of the points and find the distance between them. After that, the program will output the results in an output file. Again, this is not shown because I am not sure how to do that.

My problem is getting the program to read the input files and outting them into the output files. The test cpp file has the code for getting the input and output files to open and to put the stuff in the input and copying it to the output.

Is there a way to do this, (opening input and output files) on Microsoft visual studio?

Could someone please skim through the code and find what I am doing wrong? I would be grateful.

To clear things up:

I don't know how I would incorporate an input.txt file and an output.txt file.

The program takes the numbers in the input.txt file and calculates the distance between the points. Then it would output it in the output.txt file.

An example of the input.txt file would look like:

A 3 4 5
B 3 4 5

(those numbers would correspond to the x, y, z coords)

then the output would be:
The distance between A and B is 0

Thank you for the help.

I get the following error:
error C2451: conditional expression of type 'std::fstream' is illegal

``````if (outf1)
{
char ans;``````

You can't do that with a std::fstream - however, you can do it with an ofstream or an ifstream. It appears from the variable name, that you're only using outf1 to output data - so change the line

``fstream outf1;``

``ofstream outf1;``