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.
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);
change your first if argument from this to the one below;
1 2 3 4 5 6 7 8 9 10 11
if (a <= 0 || b <= 0 || c <= 0 && (c <= a && c <= b))
valid = false;
elseif ((a + b) > c && (c > a && c > b)){
valid = true;
}elseif (a == b && b == c && a == c){
valid = true;
}else{
valid = false;
cout << "Triangle not valid, try again." << endl;
}
}
1 2 3 4
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.
1 2 3 4 5 6 7 8 9
if((angle1 == 60) && (angle2 == 60) && (angle3 == 60))
{
cout << "This is an equalateral triangle" << endl;
}elseif((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.
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?
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.
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.
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.