Area of Triangle using 2 Functions in Header

Sep 24, 2011 at 3:37am
fixed
[/code]
Last edited on Oct 9, 2011 at 6:17pm
Sep 24, 2011 at 4:35am
With a quick look, the main problem I see, is nowhere do you call the functions isValid or areaofTriangle. before your if (true) You first would call isValid, then call areaofTriangle IF isValid returns true. Try fixing that, and if you're still in trouble, let it be known here. We'll see what else needs to be done.

EDIT: You may want to change the isValid header a bit. Instead of using && in the checks for the sides, use ||, meaning OR. The way you're doing the checking, ALL THREE values must be less than 0 to make it false, instead of just one. Maybe let it be <= also, since any side of 0 makes a triangle impossible.
Last edited on Sep 24, 2011 at 4:42am
Sep 24, 2011 at 5:53am
I'm still lost. I don't think I am "calling" the functions right. Can you help with that? I added the bool function before, but I don't know if that is what calling the function is or not.
1
2
3
4
5
	cout << isValid(side1, side2, side3);
	if (true)
		cout << "The area of the triangle is: " << areaOfTriangle << endl;						        // If it is a triangle, the area will be computed
	else if (false)
		cout << "The input provided is not possible to calculate an area." << endl;		


I made the second changes you said and I have no errors when compiling, but when I enter in the sides, I always get some weird number like 013F1091. Thanks for your help alot. Any input will be appreciated. I'm new to this.


Last edited on Sep 24, 2011 at 4:40pm
Sep 24, 2011 at 6:03am
What do you expect isValid to do? Why would it know to do that? Think like a computer... that is, stupidly.
Last edited on Sep 24, 2011 at 6:03am
Sep 24, 2011 at 11:20am
Using the:

 
if(true)


Doesn't mean that it is a triangle, it just means that the statement is true.

Try to avoid doing that.
Sep 24, 2011 at 2:33pm
What do you suggest I use? I used the true/false so it could know if my triangle was actually a triangle. I guess my problem is not knowing how to connect isValue into my program. I'm lost.
Sep 24, 2011 at 5:16pm
Change
1
2
3
4
5
cout << isValid(side1, side2, side3);
	if (true)
		cout << "The area of the triangle is: " << areaOfTriangle << endl;						       
	else if (false)
		cout << "The input provided is not possible to calculate an area." << endl;


to
1
2
3
4
	if ( isValid(side1, side2, side3))
		cout << "The area of the triangle is: " << areaOfTriangle(side1, side2, side3) << endl;						      
	else
		cout << "The input provided is not possible to calculate an area." << endl;


The value you got 013F1091 is likely the memory address of the function, you need the parenthesis and the arguments when calling a function.
Last edited on Sep 24, 2011 at 5:17pm
Sep 24, 2011 at 5:24pm
fixed
Last edited on Oct 9, 2011 at 6:18pm
Sep 24, 2011 at 5:48pm
Yes. You currently return false if ALL sides are less then 0; you should return false if ANY side is less then 0.
Sep 24, 2011 at 5:53pm
So would I put this?
1
2
	if ( side1, side2, side3 <= 0 )
	return false;


I'm not sure how to do any side. I thought || was used for or.
Sep 24, 2011 at 5:55pm
Try
1
2
3
4
5
6
7
8
9
bool isValid(double side1, double side2, double side3)
{
	if ( side1 <= 0 || side2 <= 0 || side3 <= 0)
	    return false;
        else if ((side1 + side2) > side3 && (side3 + side2) > side1 && (side1 + side3) > side2)
            return true;
	else
	    return false;
}


edit: corrected else if
Last edited on Sep 24, 2011 at 6:02pm
Sep 24, 2011 at 5:55pm
It is,
1
2
//if ( side1 < 0 && side2 < 0 && side3 < 0) //This should be
  if ( side1 < 0 || side2 < 0 || side3 < 0 )
You never posted updated code , so I wasn't sure if you had done that already. (<= would be fine too.)

EDIT: Also on your other statement you should be using &&'s
1
2
//if (side1 + side2 > side3 || side3 + side2 > side1 || side1 + side3 > side2) //should be
  if (side1 + side2 > side3 && side3 + side2 > side1 && side1 + side3 > side2)
Meaning, there is no maximum size such that the sum of the other two sides are less then it.
Last edited on Sep 24, 2011 at 5:58pm
Sep 24, 2011 at 6:00pm
fixed
Last edited on Oct 5, 2011 at 6:40pm
Sep 24, 2011 at 6:09pm
Mathhead200's edit was correct the else if should be
else if (side1 + side2 > side3 && side3 + side2 > side1 && side1 + side3 > side2)
Last edited on Sep 24, 2011 at 6:09pm
Sep 24, 2011 at 6:11pm
Naraku9333
I tried that and still get a 0 when invalid sides are inputted.

I've been using 10, 10, and 20 for my invalid sides.
Last edited on Sep 24, 2011 at 6:13pm
Sep 24, 2011 at 6:17pm
Lol, return FALSE!!! (Sorry I missed that.)
1
2
if( /*sides are not valid*/ )
    return false;
Last edited on Sep 24, 2011 at 6:18pm
Sep 24, 2011 at 6:19pm
That's it! WOOOOO! Geeze you guys are great. Ya'll really helped out. I really appreciate it!!
Topic archived. No new replies allowed.