User-Defined function

Hi I need help with my code...


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

using namespace std;

int main()
{
double periTriangle (double, double, double);
{
double X1, X2, X3, Y1, Y2, Y3;
double sideA, sideB, sideC, perimeterTriangle;
cout << "X1 = ";
cin >> X1;
cout << "Y1 = ";
cin >> Y1;
cout << "X2 = ";
cin >> X2;
cout << "Y2 = ";
cin >> Y2;
cout << "X3 = ";
cin >> X3;
cout << "Y3 = ";
cin >> Y3;

sideA = sqrt( fabs(((X2-X1)*(X2-X1))+((Y2-Y1)*(Y2-Y1))));
sideB = sqrt( fabs(((X3-X2)*(X3-X2))+((Y3-Y2)*(Y3-Y2))));
sideC = sqrt( fabs(((X3-X1)*(X3-X1))+((Y3-Y1)*(Y3-Y1))));

perimeterTriangle = periTriangle( sideA, sideB, sideC);

cout << "Perimeter = " << perimeterTriangle;
return 0;
}

double periTriangle (double sideA, double sideB, double sideC)
{
return (sideA + sideB + sideC);
}



}
my problem is on the 2nd block of my code
the compiler said a function definition is not allowed before '{'
and ';' is expected before '{'

thx ^^
You can't define a function inside a function. Just move the definition outside of main and get rid of the random bracket on line 10

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
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

double periTriangle (double, double, double);

int main()
{

double X1, X2, X3, Y1, Y2, Y3;
double sideA, sideB, sideC, perimeterTriangle;
cout << "X1 = ";
cin >> X1;
cout << "Y1 = ";
cin >> Y1;
cout << "X2 = ";
cin >> X2;
cout << "Y2 = ";
cin >> Y2;
cout << "X3 = ";
cin >> X3;
cout << "Y3 = ";
cin >> Y3;

sideA = sqrt( fabs(((X2-X1)*(X2-X1))+((Y2-Y1)*(Y2-Y1))));
sideB = sqrt( fabs(((X3-X2)*(X3-X2))+((Y3-Y2)*(Y3-Y2))));
sideC = sqrt( fabs(((X3-X1)*(X3-X1))+((Y3-Y1)*(Y3-Y1))));

perimeterTriangle = periTriangle( sideA, sideB, sideC);

cout << "Perimeter = " << perimeterTriangle;
return 0;
}

double periTriangle (double sideA, double sideB, double sideC)
{
return (sideA + sideB + sideC);
} 
Last edited on
Topic archived. No new replies allowed.