What's wrong with this code?

Hi. I trying to Write a class triangle with the following members:

a. Private variables point a, b, c representing the vertices.

b. Public constructor function triangle() that receives three parameters of type
double and uses them to initialize the private variables. The parameters default
to 0.0 if they are not provided.

c. Public function double perimeter() const that returns the perimeter of this triangle.

d. Public function double area() const that returns the area of this triangle.

e. Public function bool is_right() const that returns true if this triangle is right
triangle and false otherwise.

Provide a main function that prompts the user for three vertices, creates a triangle using
these vertices, outputs its perimeter, area, and whether it is a right triangle.




Here is my code so far. To me, it looks like it should work.

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

struct point
{
point(init_x=0.0, init_y=0.0)
{
x=init_x;
y=init_y;
}
double x, y;
};

class triangle
{
public:
triangle(point init_a, point init_b, point init_c);
double perimeter();
double area();
bool is_right();

private:
point a, b, c;
};

triangle::triangle(point init_a, point init_b, point init_c)
{
a = init_a;
b = init_b;
c = init_c;
}

double triangle::perimeter()
{
double m, n, o, perimeter;

m = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
n = sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
n = sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));

perimeter = m + n + o;

return perimeter;
}

double triangle::area()
{
double m, n, o, area, p;

m = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
n = sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
o = sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));
p = (m + n + o)/2.0;

area = sqrt(p(p-m)(p-n)(p-o));

return area;
}

bool triangle::is_right()
{
double m, n, o;

m = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
n = sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
o = sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));

if (m >= n || m >= o || ((o*o)+(n*n)==(m*m)))
return true;
else if (n >= m || n >= o || ((o*o)+(m*m)==(n*n)))
return true;
else if (o >= n || o >= m || ((m*m)+(n*n)==(o*o)))
return true;
else
return false;
}

int main(void)
{
point a(2, 3), b(4, 5), c(6, 7);

triangle t(a, b, c);

cout << t.perimeter() << endl;
}

I guess struct don´t like functions
I guess struct don´t like functions


In C++ struct and class are identical but for default privacy levels. Both can have functions, because they're the same thing.
I never saw a struct with a function in it give me an example

I never saw a struct with a function in it give me an example


Here is a struct that has two member functions.

1
2
3
4
5
struct Rectangle {
    int x, y;
    void set_values (int,int);
    int area (void);
  };
Last edited on
closed account (D80DSL3A)
@knguyen252
You haven't calculated o in the perimeter().
do it in the main();
As fun2code says, this

1
2
3
m = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
n = sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
n = sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));


looks like it should actually be

1
2
3
m = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
n = sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
o = sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));
but I think it is simply 1*1=1;
Topic archived. No new replies allowed.