Need help.

Can someone tell me what is wrong here? Im trying to make a program that reads the legnth of sides of triangle and outputs the area of the triangle. I need to use function for area (area = sqrt[s(s-a)(s-b)(s-c)] and for s (s=(a+b+c)/2).
It says that 's' cannot be used as a function.

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
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
double s_triangle (double a, double b, double c);
double a_triangle (double s, double a, double b, double c);
int main()
{
    double a,b,c,s,area;
    cout<<"Enter Length of First side: ";
    cin>>a;
    cout<<"Enter Length of Second side: ";
    cin>>b;
    cout<<"Enter Length of Third side: ";
    cin>>c;
    s=s_triangle (a,b,c);
    area=a_triangle (s,a,b,c);
    cout<<"The Area of the Triangle is "<<area<<".\n";
    return 0;
}
double s_triangle (double a, double b, double c){
    return ((a+b+c)/2);
}
double a_triangle (double s,double a,double b,double c){
    return (sqrt(s(s-a)(s-b)(s-c)));
}

Last edited on
c++ does not understand implicit multiply as seen in math text books. That is, x(b) is not able to multiply x and b -- c++ sees this as a function call for function x with a parameter of b.

you must explicitly use the * to multiply:

(sqrt(s(s-a)(s-b)(s-c)));
should be

( sqrt( s*(s-a)*(s-b)*(s-c)) )

also its <cmath> not <math.h>. Math.h lacks namespace and is for C code.

Last edited on
Oh, I see. Thanks for the quick reply. Works fine now.
Topic archived. No new replies allowed.