program representing an axis-aligned right triangle (classes)

I am still learning the concept of classes and I'm a little confused. I keep getting the error Segmentation fault (core dumped). Can anyone please explain why? I know this error usually deals with pointers but I don't see where that would be the issue

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <cmath>
using namespace std;

class Point
{
private:
 double px;
 double py;

public:
 void setX(const double x);
 void setY(const double y);
 double getX() const;
 double getY() const;
};

class Triangle
{
private:
 Point blPoint;
 double length, height;

public:
 // member functions
 void setBottomLeftX(const double x);
 void setBottomLeftY(const double y);
 void setLength(const double inLength);
 void setHeight(const double inHeight);
 Point getBottomLeft() const;
 Point getBottomRight() const;
 Point getTopLeft() const;
 double getLength() const;
 double getHeight() const;
 
 double perimeter() const;
 double hypotenuse() const;
 void scaleLength(const double sx);
 void scaleHeight(const double sy);
 void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
void read_triangle(Triangle & tri);
int main()
{
 // Define local variables
 Triangle tri;
 double sx, sy;
 
 //Prompt the user for triangle information and fill Class Triangle object, tri,
 //with this information
 read_triangle(tri);
 
 // Display triangle information
 tri.display();
 
 // Prompt and read scale factors to change length and height
 cout << "Enter scale factor in x direction: ";
 cin >> sx;
 
 cout << "Enter scale factor in y direction: ";
 cin >> sy;
 
 // Apply scale factors
 tri.scaleLength(sx);
 tri.scaleHeight(sy);
 
 // Display triangle information
 tri.display();
 
 return 0;
}
   
// FUNCTION DEFINITIONS GO HERE:
    
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:
void Point::setX(const double x) 
{
  px = x;
}
void Point::setY(const double y) 
{
  py = y;
}

double Point::getX() const 
{
  return (px);
}

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

void Triangle::setBottomLeftX(const double x) 
{
  blPoint.setX(x);
}

void Triangle::setBottomLeftY(const double y) 
{
  blPoint.setY(y);
}

void Triangle::setLength(const double inLength) 
{
  length = inLength;
}

void Triangle::setHeight(const double inHeight) 
{
  height = inHeight;
}

Point Triangle::getBottomLeft() const
{
  return blPoint;
}

Point Triangle::getBottomRight() const
{
  double mx = (blPoint.getX() + height);
  getBottomRight().setX(mx);
  return getBottomRight();
}

Point Triangle::getTopLeft() const
{
  double my = (blPoint.getY() + height);
  getTopLeft().setY(my);
  return getTopLeft();
}

double Triangle::getLength() const
{
  return length;
}

double Triangle::getHeight() const
{
  return height;
}

double Triangle::hypotenuse() const
{
  return sqrt((length * length) + (height * height));
}
 
double Triangle::perimeter() const
{
  return ((sqrt(length * length) + (height * height)) + length + height);
}
 
void Triangle::scaleLength(const double scalefact) 
{
  length *= scalefact;
}

void Triangle::scaleHeight(const double scalefact) 
{
  height *= scalefact;
}

void Triangle::display() const
{
  cout << "----------------------------------------" << endl;
  cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ")" << endl;
  cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft().getY() << ")" << endl;
  cout << "Bottom Right Vertex (" << getBottomRight().getX() << ", " << blPoint.getY() << ")" << endl;
  cout << "Dimensions (" << getBottomRight().getX() - blPoint.getX() << ", " << getTopLeft().getY() - blPoint.getY() << ")" << endl;
  cout << "Hypotenuse = " << hypotenuse() << endl;
  cout << "Perimeter = " << perimeter() << endl;
}

void read_triangle(Triangle & tri)                                          // reads in coordinates of a triangle as well as its length and height, Triangle tri is class containing information about the triangle
{
  double x, y, inLength, inHeight;
  cout << "Enter bottom left x coordinates: ";
  cin >> x;
  tri.setBottomLeftX(x);
  cout << "Enter bottom left y coordinates: ";
  cin >> y;
  tri.setBottomLeftY(y);
  cout << "Enter length: ";
  cin >> inLength;
  tri.setLength(inLength);
  cout << "Enter height: ";
  cin >> inHeight;
  tri.setHeight(inHeight);
}
Last edited on
An invalid pointer is not the only reason for a segmentation fault. You can also get a segmentation fault from infinite recursion.

Lines 122-126: getBottomRight is infinitely recursive. This function will just keep calling itself and never exit until it traps when you've exhausted the stack. A good compiler should have warned you of this.

Lines 129-133: ditto

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Last edited on
How should I go about fixing the infinite recursion? I'm pretty lost right now and can't find where to fix. I also know I have to edit the code in lines 170-172 but I don't know what to do there either.
What about this?
122
123
124
125
126
127
128
129
Point Triangle::getBottomRight() const
{   Point   br;     //  bottom right

    double mx = (blPoint.getX() + height);
    br.setX(mx);
    br.setY(blPoint.getY());
    return br;
}

Topic archived. No new replies allowed.