# Thread: Triangle Perimeter (nest structure)Issue

1. ## Triangle Perimeter (nest structure)Issue

Hey, guys. I am trying to write a code that can calculate triangle perimeter. I am using nest structure to write it. Build is fine 0 error. But my result is weird always prints out some characters combine with number instead of real results. This is my code below...I am not sure which part goes wrong, pleassseee help~~~
Code:
```#include <stdio.h>
#include <math.h>
#define n 3
struct points
{
double x;
double y;

}; //set up 3 point for a triangle
struct perimeter
{
struct points point[n];

double tri_perimeter;
}tri_p;
double cal_perimeter(struct perimeter *);
int main(void)
{
int i;
struct points triangle[n];
struct perimeter tri_p;
for(i=0; i<n; i++)
{
printf("Please enter point %d of x:", i+1);
scanf("%lf", &triangle[i].x);
printf("Please enter point %d of y:", i+1);
scanf("%lf", &triangle[i].y);
}
tri_p.tri_perimeter=cal_perimeter(&tri_p);
printf("Result is: %lf", tri_p.tri_perimeter);
return 0;
}
double cal_perimeter(struct perimeter *perimeter_ptr)
{

double side_length_1;
double side_length_2;
double side_length_3;
side_length_1=sqrt(pow((perimeter_ptr->point[0].x-perimeter_ptr->point[1].x),2)+pow((perimeter_ptr->point[0].y-perimeter_ptr->point[1].y),2));
side_length_2=sqrt(pow((perimeter_ptr->point[1].x-perimeter_ptr->point[2].x),2)+pow((perimeter_ptr->point[1].y-perimeter_ptr->point[2].y),2));
side_length_3=sqrt(pow((perimeter_ptr->point[2].x-perimeter_ptr->point[0].x),2)+pow((perimeter_ptr->point[2].y-perimeter_ptr->point[0].y),2));
perimeter_ptr->tri_perimeter=side_length_1+side_length_2+side_length_3;
return perimeter_ptr->tri_perimeter;
}```

2. It works now..sorry, I was stupid, should not declare structure points as triangle[n]....totally mislead...here is the working code
Code:
```#include <stdio.h>
#include <math.h>
#define n 3
struct points
{
double x;
double y;

}; //set up 3 point for a triangle
struct perimeter
{
struct points point[n];

double tri_perimeter;
}tri_p;
double cal_perimeter(struct perimeter *);
int main(void)
{
int i;

for(i=0; i<n; i++)
{
printf("Please enter point %d of x:", i+1);
scanf("%lf", &tri_p.point[i].x);
printf("Please enter point %d of y:", i+1);
scanf("%lf", &tri_p.point[i].y);
}
tri_p.tri_perimeter=cal_perimeter(&tri_p);
printf("Result is: %lf", tri_p.tri_perimeter);
return 0;
}
double cal_perimeter(struct perimeter *perimeter_ptr)
{

double side_length_1;
double side_length_2;
double side_length_3;
side_length_1=sqrt(pow((perimeter_ptr->point[0].x-perimeter_ptr->point[1].x),2)+pow((perimeter_ptr->point[0].y-perimeter_ptr->point[1].y),2));
side_length_2=sqrt(pow((perimeter_ptr->point[1].x-perimeter_ptr->point[2].x),2)+pow((perimeter_ptr->point[1].y-perimeter_ptr->point[2].y),2));
side_length_3=sqrt(pow((perimeter_ptr->point[2].x-perimeter_ptr->point[0].x),2)+pow((perimeter_ptr->point[2].y-perimeter_ptr->point[0].y),2));
perimeter_ptr->tri_perimeter=side_length_1+side_length_2+side_length_3;
return perimeter_ptr->tri_perimeter;
}```