how do i display x and y

I have TXT file that's supposed to be open and read.
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]
Point2D, [3, 2]

I am unable to display X1, X2, Y1, Y2 when cout...

Line2D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <limits>
#include <iomanip>
#include <cmath>
#include "Line2D.h"
#include "Point2D.h"

using namespace std;

Line2D::Line2D()
{
    pt1 = Point2D();
    pt2 = Point2D();
}

Line2D::Line2D(const Point2D point1, const Point2D point2)
{
    pt1 = point1;
    pt2 = point2;
    setLength();
    length = getScalarValue();
}

Point2D Line2D::getPt1() const
{
    pt1.Point2D::getX();
    pt1.Point2D::getY();
    return pt1;
}

void Line2D::setPt1(Point2D PT1)
{
    this -> pt1 = PT1;
    setLength();
}

Point2D Line2D::getPt2() const
{
    pt2.Point2D::getX();
    pt2.Point2D::getY();
    return pt2;
}

void Line2D::setPt2(Point2D PT2)
{
    this -> pt2 = PT2;
    setLength();
}

double Line2D::getScalarValue() const
{
    return length;
}

void Line2D::setLength()
{
    length = sqrt(pow((pt1.getX() - pt2.getX()),2) + pow((pt1.getY() - pt2.getY()),2));
}

ostream & operator << (ostream & out, const Line2D& l2d)
{
    out << '[' << l2d.pt1.getX() << ',' << "\t\t" << l2d.pt2.getY() << ']' << "\t\t " << '[' << l2d.getPt2().getX() << ',' << "\t\t" << l2d.pt2.getY() << ']' << "\t\t" << fixed << setprecision(3) << l2d.getScalarValue() << '\n';
    return out;
}

istream & operator >> (istream& in, Line2D& l2d)
{
    char left = ' ', comma= ' ', right = ' ';
    in >> left >> l2d.x >> comma >> l2d.y >> right;
    
    if (!(left == '[' && comma == ',' && right == ']'))
        in.setstate(ios::failbit);
    return in;
}

bool Line2D::operator < (const Line2D& rhs) const
{
    if (x < rhs.x)
        return true;
    if ((x == rhs.x) && (y < rhs.y))
        return true;
    return false;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void read_in_data()
{
    string classname, str;
    int count = 1;
    cout << "Please Enter Filename : ";
    cin >> filename;
    myfile.open(filename.c_str(),std::ios::in);
    if(ifs.is_open())
    {
    while (getline(ifs, classname, ',') && getline(ifs, str, '\n'))
        if (classname == "Line2D")
        {
            stringstream coordinates(str);
            Line2D l2d;
            while (coordinates >> l2d)
            {
                linetwod.insert(l2d);
                count++;
            }
        }


Line2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef Line2D_h
#define Line2D_h

#include <stdio.h>
#include <iostream>
#include <string>
#include "Point2D.h"

class Line2D : public Point2D
{
private:
    Point2D pt1;
    Point2D pt2;

protected:
    double length;
    void setLength();

public:
    Line2D();
    Line2D(Point2D, Point2D);
    Point2D getPt1() const;
    Point2D getPt2() const;
    double getScalarValue() const;

    void setPt1(Point2D);
    void setPt2(Point2D);
    
    friend istream & operator >> (istream & is, Line2D & l2d);
    friend ostream & operator << (ostream & os, const Line2D & l2d);
    bool operator < (const Line2D & rhs) const;
};
#endif 


Point2D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Point2D.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <limits>
#include <iomanip>
#include <cmath>

using namespace std;

Point2D::Point2D() : x(0), y(0), distFrOrigin(0.000)
{
}

Point2D::Point2D(int X, int Y)
{
    x = X;
    y = Y;
}

int Point2D::getX() const
{
    return x;
}

void Point2D::setX(int set_x)
{
    x = set_x;
}

int Point2D::getY() const
{
    return y;
}

void Point2D::setY(int set_y)
{
    y = set_y;
}

double Point2D::getScalarValue() const
{
    return sqrt((x * x) + (y * y));
}

void Point2D::setDistFrOrigin()
{
    distFrOrigin = sqrt((x * x) + (y * y));
}

ostream & operator << (ostream & out, const Point2D & p2d)
{
    out << '[' << p2d.x << ',' << "\t\t" << p2d.y << ']' << "\t\t " << fixed << setprecision(3) << p2d.getScalarValue() << '\n';
    return out;
}

istream & operator >> (istream& in, Point2D & p2d)
{
    char left = ' ', comma= ' ', right = ' ';
    in >> left >> p2d.x >> comma >> p2d.y >> right;
    
    if (!(left == '[' && comma == ',' && right == ']'))
        in.setstate(ios::failbit);
    return in;
}

bool Point2D::operator < (const Point2D & rhs) const
{
    if (x < rhs.x)
        return true;
    if ((x == rhs.x) && (y < rhs.y))
        return true;
    return false;
}


Why couldn't i get my program to display Point1 X, Point1 Y, Point2 X, Point2 Y during my cout...
Last edited on
Hello mrmartin93,

You are missing the file Point2D.h and the function in the section marked main.ccp looks more like a regular function rather than main. With out the proper code I can not test to see what is happening. My guess right now is the problem is in the overloaded operator << or in where it is called.

The code in what you call main.ccp or read_in_data() function has several problems including variables that are not defined.

Hope that helps,

Andy

Sorry for the short message. Having problems with my internet connection.
As mentioned previously, Line2D is not a type of Point2D, it should not inherit from Point2D. (or if it did, which would seem a strange design, then it would not need both of pt1 and pt2).

http://www.cplusplus.com/forum/general/201597/2/#msg962499
Topic archived. No new replies allowed.