You are using a version without Ads of this website. Please, consider donating:

getArea function is only returning 0

I'm doing an assignment where i need to have a user create a triangle and I have a separate .cpp file which contains my triangle class and methods used to find area, length of sides they enter, etc.

My methods are working except my getArea one which returns 0 no matter what and I can't seem to figure out why. The method takes in two side lengths (height and width), multiplies them by 1/2 and returns this value, except it will only return 0 even if my lengths are say 3 and 4 (it should return 6). I've included my main.cpp file below along with my triangle.cpp file below. getMain is the method I'm having trouble with, any help is appreciated thank you.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include "Triangle.h" using namespace std; int main() { double a, b, c, d, e, f; bool valid; // Triangle triangleUser();, while (valid == false){ cout << "Enter lengths for the sides of your triangle in ascending order: " << endl; cout << "side 1: "; cin >> a; cout << "side 2: "; cin >> b; cout << "hypotenuse: "; cin >> c; // Triangle triangleUser(a, b, c); if (a <= 0 || b <= 0 || c <= 0 && (c <= a && c <= b)) valid = false; else if ((a + b) > c && (c > a && c > b)){ valid = true; }else if (a == b && b == c && a == c){ valid = true; }else{ valid = false; cout << "Triangle not valid, try again." << endl; } } if(valid == true){ double side1 = a; double side2 = b; double side3 = c; Triangle triangleUser(a, b, c); //triangleUser.getInfo(); //triangleUser.triangle_type(); triangleUser.getArea(side1, side2); } return 0; }``````

and here is triangle.cpp which includes my .h file that I won't include in here unless asked for

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````//#ifndef TRIANGLE_H_INCLUDED //#define TRIANGLE_H_INCLUDED #include #include #include "Triangle.h" using namespace std; /* int side1; int side2; int side3; int angle1; int angle2; int angle3; */ Triangle::Triangle(){ side1 = 3; side2 = 4; side3 = 5; angle1 = 90; angle2 = 45; angle3 = 45; } Triangle::Triangle(double newSide1, double newSide2, double newSide3) { side1 = newSide1; side2 = newSide2; side3 = newSide3; } double Triangle::getInfo(){ cout << "side 1 is : " << side1 << " side 2 is: " << side2 << " side 3 is: " << side3 << endl; return 0; } bool Triangle::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; } double Triangle::getArea(double side1, double side2){ double area = (1/2)*(side1*side2); cout << area << endl; return area; } int Triangle::triangle_type(){ if((angle1 == 60) && (angle2 == 60) && (angle3 == 60)) { cout << "This is an equalateral triangle" << endl; return 0; } else if((angle1 == 90) || (angle2 == 90) || (angle3 == 90)) { cout << "This is a right triangle" << endl; return 0; }else{ cout << "You have an isosceles triangle" << endl; return 0; } }; ``````
 `` `` ``double area = (1/2)*(side1*side2);``

(1/2) divides the integer 1 by the interger 2, thus resulting in 0, not .5
The problem is the way C++ interprets the equation you wrote.
` (1/2)*(side1*side2); `

Seeing as 1/2 is dividing one integer by another integer the result is going to be an integer. It would normally be 0.5 but seeing as an int is only whole numbers it will simply be 0. So of course anything times 0 is 0.

There are a few solutions to this, but you want to make sure it's seen as a double and not an int when multiplying. You could either type cast it to a double or use one of these.
` 0.5*(side1*side2); ` ` 1.0/2.0*(side1*side2); `
Your code has undefined behaviour because variable valid was not initialized.

 ``123`` `````` bool valid; // Triangle triangleUser();, while (valid == false){``````

What is the initial value of 'valid'?
Thank you for replies guys, i didn't realize that about the (1/2) equating to 0.
change your first if argument from this to the one below;
 ``1234567891011`` `````` if (a <= 0 || b <= 0 || c <= 0 && (c <= a && c <= b)) valid = false; else if ((a + b) > c && (c > a && c > b)){ valid = true; }else if (a == b && b == c && a == c){ valid = true; }else{ valid = false; cout << "Triangle not valid, try again." << endl; } }``````

 ``1234`` `````` if (a <= 0 || b <= 0 || c <= 0 || c < a || c < b) valid = false; } }``````

You don't want to exclude a triangle where c == a or c == b because that would exclude equalateral triangles and your else if() arguments won't pick that up later on.
everything is working properly except on my triangle type now.

 ``123456789`` ``````if((angle1 == 60) && (angle2 == 60) && (angle3 == 60)) { cout << "This is an equalateral triangle" << endl; }else if((angle1 == 90.0) || (angle2 == 90.0) || (angle3 == 90.0)) { cout << "This is a right triangle" << endl; }else{ cout << "This is an equalateral triangle" << endl; }``````

why does this not work? my angle 3 = 90 when i use values 3, 4 and 5 for sides. I calculate angles with methods like the following and it gives me the right values.

 ``12345678`` ``````double Triangle::getAngle3(double side1, double side2, double side3){ double angle3 = ((side1*side1) + (side2*side2) - (side3*side3)) / (2*side1*side2); angle3 = acos(angle3) * 180.0 / PI; return angle3; }``````

Your first and last option there output the same thing (This is an equalateral traingle), though in your original code they said different things. By the way, it's spelled equilateral, not equalateral. How exactly is this function not working?
sorry the last one is meant to say isosceles. Why is the 2nd one not working when I have an angle that = 90 though? it should say "this is a right triangle" shouldn't it?
Last edited on
Well, comparing floating point values for equality may be problematic.
Sometimes there may be a minor difference in the last significant digit, for example instead of 1.0 you get 0.99999999999999 or 1.0000000000001.
You may need to use some method of rounding the value prior to comparison to see if it == 90.0 degrees.

What value did you use for PI? Its accuracy will affect the conversion of radians to degrees and vice versa.
I used 3.14159265
That would explain it. You could try using a more precise value for PI, such as 3.1415926535897932 but that isn't guaranteed to make the problem go away.

Try this.
Instead of testing `(angle == 90.0)`, try
`(fabs(angle -90.0) < 1.0E-10)`.

That is, the absolute value of the discrepancy between the two values is less than some very small number. You can fine-tune the value you use as required.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: