-1

I am trying to create a program that can use relationships to determine the type of triangle being calculated. I have tried float and double declarations but either way the end result is incorrect and is draving me crazy. Basically the program does not regognize "c" as being equal to a/b for every like value.(Ex. enter 7 7 for sides a and b, and 60 for angle, should yield an equilateral, but does not.) Any ideas as to how I might address this? Thank you so much!

#include <iostream>
#include <cmath>
#define pi 3.14159265358989

using namespace std;

int main()
{
float  a, b, c, d, C;


cout << "Hello. I can calculate the missing length of a triangle" << endl;
cout << "by using using the information you provide." << endl;
cout << "Please enter two positive integers between 1-99 and separate them with a space. " << endl;
cin >> a >> b;
cout << "Side 1 equals: " << a << endl;
cout << "Side 2 equals: " << b << endl;
cout << "Please enter the angle between the two sides you just entered. " << endl;
cin >> d;

C = d * (pi / 180); // convert rad to deg
//Compute missing side
c = sqrt((a*a) + (b*b) - 2 * a * b * cos(C));
cout << "The length of the third side of the triangle is: " << c << endl;

if (a == b && b == c) //all sides are equal
    cout << "This is an Equilateral Triangle." << endl;

else if (a == b && b != c)  // if 2 sides are equal

    cout << "This is an Isosceles Triangle. " << endl;

else if (a != b && b != c) // if no sides are equal

    cout << "This is a Scalene Triangle. " << endl;


    system ("pause");
    return 0;

}

GamerJ
  • 3
  • 1
  • 2
    Is the issue that `c` is like `7.00001` or that it's completely way off? If the former, see [comparing two floats](http://stackoverflow.com/q/17333/2069064) – Barry Jan 31 '15 at 23:07
  • I'm not 100% sure. As I ask the program to tell me the values and they are all the same but the (a == b && b == c) does not agree. I am pretty new to programming but I am assuming that would be the issue. Thanks! – GamerJ Jan 31 '15 at 23:18

1 Answers1

0

You cannot really use == to compare float or double, at least not in any meaningful way for your example.

You have to specify what 2 float being equals with each other means. i.e : what is the epsilon difference between 2 floats that is small enough for you to consider them equal.

See this.

What is the most effective way for float and double comparison?

Community
  • 1
  • 1
Félix Cantournet
  • 1,941
  • 13
  • 17