Reputation: 103
here's the simple code I'm working on. The objective of the code is to accept three float values and then figure out what type of triangle it is; equilateral, isosceles, scalene, right angled, or a combination of those.
#include <iostream>
using namespace std;
int main(){
float side_a, side_b, side_c;
int equi, iso, sca, right_angle;
int equi_same, iso_same, sca_same, right_angle_same;
cin >> side_a >> side_b >> side_c;
if( (side_a == side_b) && (side_b == side_c) && (side_a == side_c ) ){
cout << "\nThis is an equilateral triangle";
equi = 1;
iso = 0;
sca = 0;
right_angle = 0;
equi_same = 1;
}
else if((side_a == side_b) || (side_b == side_c) || (side_b == side_c)){
cout << "\nThis is an isosceles triangle";
iso = 1;
equi = 0;
sca = 0;
right_angle = 0;
iso_same = 1;
}
else if( ( (side_a*side_a) == ( (side_b*side_b) + (side_c*side_c) ) ) ||
( (side_b*side_b) == ( (side_a*side_a) + (side_c*side_c) ) ) ||
( (side_c*side_c) == ( (side_a*side_a) + (side_b*side_b) ) ) ){
cout << "\nThis is a right angled triangle";
right_angle = 1;
equi = 0;
iso = 0;
sca = 0;
right_angle_same = 1;
}
else{
cout << "\nThis is a scalene triangle";
sca = 1;
equi = 0;
iso = 0;
right_angle = 0;
sca_same = 1;
}
if( (equi = 1) && (equi_same != 1) ){
cout << " and an equilateral triangle";
}
else if((iso = 1) && (iso_same != 1)){
cout << " and an isosceles triangle";
}
else if((right_angle = 1) && (right_angle_same != 1)){
cout << " and a right angled triangle";
}
else if((sca = 1) && (sca != 1)){
cout << " and a scalene triangle";
}
else{
cout << "\n";
}
return 0;
}
What I'm having a hard time with is how to make the code find out that it's also a combination of other triangles. After writing the code I realised that if the first statement is true then the code won't check the other else if statements. Are there any errors that I've missed and not noticed? If no, then how should I change my code? Thanks in advance!
EDIT: In the test run, if I input the same numbers the output will be:
It is an equilateral triangle and a right angled triangle
But if I put in numbers that fulfil the other requirements, output:
It is a (depends on input) triangle and an equilateral triangle
Maybe I did something wrong at the right angle part or equilateral part or is there something else I missed?
Upvotes: 2
Views: 2171
Reputation: 600
bool FIsosceles(double a, double b, double c)
{
return ((a == b) || (a == c) || (b == c));
}
bool FEquilaterlal(double a, double b, double c)
{
return ((a == b) && (a == c));
}
bool FRightAngle(double a, double b, double c)
{
return ((a > b) && ( a > c)) ? (a*a == b*b + c*c) : (b > c ? (b*b == a*a + c*c) : (c*c == b*b + a*a));
}
int main()
{
double side_a, side_b, side_c;
cin >> side_a >> side_b >> side_c;
if(FIsosceles(side_a, side_b, side_c))
{
cout << " and an isosceles triangle";
if(FEquilateral(side_a, side_b, side_c))
{
cout << " and an equilateral triangle";
}
else if(FRightAngle(side_a, side_b, side_c))
{
cout << " and right triangle";
}
}
else if(FRightAngle(side_a, side_b, side_c))
{
cout << " and right triangle";
}
else
{
cout << " Either an scalene triangle or not a triangle";
}
}
Upvotes: 0
Reputation: 23
You are using the "else if" case for this program. Once the first condition is true, the compiler skips all the test cases mentioned below. Safest bet would be using multiple if() conditions and assigning boolean flags to the triangles.
Example:
if(equilateral)
bool Equilateral=true;
if(isoceles)
bool Isoceles=true;
if(rightAngled)
bool RightAngled=true;
Now you can check for right angled isoceles condition and if true, you can set its flag to true.
Finally you write specific test cases to find out what the triangle actually is.
Upvotes: 0
Reputation: 2539
You are overcomplicating it. Try to create different functions that determine if the triangle falls in a type, and flag it, and use == instead of =:
In your first test do not write else if
-s, just if
s. Do not output the results, just set the flags.
Later check the flags, and write the results.
if(Equi(side1, side2, side3))
equi = 1;
if(Iso(side1, side2, side3))
iso = 1;
if(Sca(side1, side2, side3))
sca = 1;
if(RightAngled(side1, side2, side3))
rang= 1;
std::cout << "This is a " << equi == 1 ? "equilateral" : "" << ... << " triangle.";
EDIT: with using namespace std
you can omit the std::
in the beginning.
The equi == 1 ? "equilateral" : ""
is a conditional expression:
Before the ?
mark there is a condition, if it is true, it returns the first side of the "equilateral" : ""
expression, if false, an empty string (it is the ""). So it only outputs something if the flag is set. Check out http://www.cplusplus.com/forum/articles/14631/
Example:
// condition ? if_true_return_this : if_false_return_this
int x = true ? 1 : 2; //x will be 1
x = ((x == 2) ? 0 : 3); //x will be 3, since x == 2 is false, so the second in ... : ... is returned.
The Equi(...)
and the others are functions, you can replace them with your statements in you first part, I just did not want to copy it for a more compact code.
I hope it helps.
Upvotes: 2
Reputation: 8805
Given the sides of a triangle, you want to know
what type of triangle it is; equilateral, isosceles, scalene, right angled, or a combination of those
If you first consider the four types of angle classification:
then consider if it's a right-angled triangle (if it's isosceles or scalene), the code will be easier to write and understand.
You're testing for a right-angled triangle in the same chain of if
statements as isosceles and scalene, but you can be a right-angled scalene or a right-angled isosceles.
Upvotes: 0
Reputation: 1
Isosceles can be Right angel triangle you need to put same condition with if statement with in the isosceles triangle condition
Upvotes: 0