Hi I need to create a void function to calculate area and circumfernce of triangle.
So far I did this:
void area(int a, int b, int c )
{
using namespace std;

int area;
int circ;
int s;
s = ((a + b + c)/2);
if ((a + b > c) && (b + c > a) && (a + c > b))
{
area = sqrt(s(s-a)(s-b)(s-c));
}
else
cout << "Its not a triangle" << endl;
}
int main() {

int side1, side2, side3;

cout << "Enter three lengths of the triangle sides: ";
cin >> side1 >> side2 >> side3;

cout << "The area of the triangle is " << area(side1, side2, side3) << endl;
return 0;
}

but it doesn't want to work!
What I am doing wrong?
Please help!!!

Recommended Answers

All 17 Replies

what errors are you getting if any?
what output are you getting if any?
what's the freakin problem?

I have 2 errors:
1. 's' cannot be used as a function
2. `sqrt' undeclared (first use this function)

Please don't use bold text when you post. It's meant to give emphasis to parts of a post, not the entire body; using it too much is poor netiquette. Also, please use [code] and [/code] tags when posting code excerpts.

The problem is on this line: area = sqrt(s(s-a)(s-b)(s-c)); You have to use * to multiply things. If you just have the parentheses, the compiler thinks you're trying to call a function. And for sqrt, put #include <cmath> at the top of your file.

Circumference of a Triangle ? WTF !!!
Now ... why your program didnot Run.

using namespace std;

should be Global in scope . Meaning outside block of any function. If you do not define the namespace globally, u will have to use std::cin , std::cout inside every function.

area = sqrt(s(s-a)(s-b)(s-c));

1st , s*(s-a)*(s-b)*(s-c) is the correct way.
In CPP , product of two variables is signified by a*b , * is the multiplication operator.
2nd. sqrt is a library function found in the header file cmath,

double sqrt (      double x );
float sqrt (       float x );
long double sqrt ( long double x )

So s, a , b ,c must be defined as float.
consequently, in the main , side1,side2,side3 should also be defined as float.
or double as u choose.

Now lets come to the main.

cout << "The area of the triangle is " << area(side1, side2, side3) << endl;

area is a void function.. returns nothing. so printing it in the console is moot point.
Had area been a funtion having return type as float, then the above code may had some effect provided u had returned the area ffrom the function.

the modiefied code can look something like :

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void area(float a, float b, float c )
{
float area;
float s;
s = ((a + b + c)/2); 
if ((a + b > c) && (b + c > a) && (a + c > b))
{
area = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\nArea ="<<area<<"\n";
}
else
cout << "Its not a triangle" << endl;
}
int main() 
{
float side1, side2, side3;
cout << "Enter three lengths of the triangle sides: ";
cin >>side1>>side2 >>side3;
area(side1, side2, side3);
cin.get();
return 0;
}

Thanks A Lot !!!
Works Perfect!:)

Circumference of a Triangle ? WTF !!!

Didn't you learn anything in school -- its a tringle with rounded corners:)

"Triangle with Rounded Corners" ? I do not get it.
I think what he meant was the perimeter of the Triangle.
There is no such technical term as Circumference of a Triangle. There is however the Circumference of a Circle.
With all due respect Ancient Dragon, I think you should get your facts checked before berating someone, what with your stature here. It doesn't look good.
However if someone means the Perimeter of a Triangle , calling it the circumference would be incorrect.

commented: Not cool. -2

>"Triangle with Rounded Corners" ? I do not get it.
It's called a joke. Chill out.

For me it was also shocking but It's what they want!!!
Enjoy!

maybe they mean the circumfrenece of the circle that surrounds the triangle ?

Suprisingly, it's the largest circle that could be drawn inside of a triangle which is the circumference of a triangle. It'd dumb but i've seen it before.

which measures the circumference of the triangle...

Any ways, what u desire is the circumference of the incircle ? Right ?

>"Triangle with Rounded Corners" ? I do not get it.
It's called a joke. Chill out.

A circle has circumference, but a triangle has a perimeter. (Actually, any closed figure has a perimeter)

With all due respect Ancient Dragon, I think you should get your facts checked before berating someone, what with your stature here. It doesn't look good.

and berating someone for making a joke (note the smiley he used: :) -- that's a BIIIIG clue) looks worse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.