/* A program that calculates
the area of a triangle using
three distinct formulas*/
#include <iostream>
#include <cmath>
usingnamespace std;
int areaOfTriangle(); // user-defined function prototype
int main()
{
areaOfTriangle();
return 0;
}
int areaOfTriangle() //user-defined function
{
float A; // variable declaration for side A
float C; // variable declaration for side C
float B; // variable declaration for breathe
float H; // variable declaration for Height
float X; // variable declaration for included angle in the triangle
int Area;
float S = (A + B + C)/2;
int ch;
cout << " MENU:\n please select the following options below\n "; // output stream object for interactive usage of program
cout << " n1. Option 1 [Hint]: A= 1/2 * B * H\n";
cout << " n2. Option 2 [Hint]: A = ABcosX\n";
cout << " n3. Option 3 [Hint]: A = squareroot of S(S-A)(S-B)(S-C)\n";
cin >> ch; // input stream object to enable user choose options
switch(ch)
{
case 1:
{
cout << "plese enter values for sides B and H: ";
cin >> B;
cin >> H;
Area = 0.5 * B * H;
break;
}
case 2:
{
cout << "Please enter values for sides A and B and the included angle: ";
cin >> A;
cin >> B;
cin >> X;
Area = A * B *cos(X);
break;
}
case 3:
{
cout << "Please enter values for A, B and C: ";
cin >> A;
cin >> B;
cin >> C;
S = (A + B + C)/2;
Area = sqrt(S*(S-A)*(S-B)*(S-C));
break;
}
default: cout << "The option chosen is a wrong one. please read the instructions and follow suit ";
break;
}
cout << "The area of the triangle is: " << Area << endl;
}
A few points you should address:
1. Why Area is an int? It does not make sense to multiply floats and the answer to be int
2. On line 26, you define S in terms of A,B,C, but those values are not defined at that point.
3. Is the X angle in degrees? Then you should convert the input to radians. The definition of the trigonometric functions in <cmath> use radians. http://www.cplusplus.com/reference/cmath/cos/
4. The formula for case 2 is wrong. If I have a right angle triangle, with A=B=1, the area should be A*B/2=0.5. If you insert X=90 degrees, cos(90 degrees)=0, so you get 0.