Function problem with triangles.

Help would definitely be appreciated! The problem is:

Three numbers are sides of a triangle if all numbers are positive and the sum of any two sides is longer than the remaining side. A triangle could be:
1 equilateral (if all three sides are equal)
2 isosceles (if any two sides are equal)
3 scalene (if none of the three sides are equal)
Write a program that inputs 5 sets of three potential sides (double data type). For each set of data, the main function calls the following three functions having the prototypes:
bool isTriangle (double, double, double);
void getType (double, double, double);
double getArea (double, double, double);
The isTriangle function returns true if the corresponding set of three numbers could be sides of a triangle or false if they do not make a triangle. If the three sides do not make a triangle, the main function must display “Not a triangle” and get the next set of data.
If they form a triangle, then the main function calls the getType function. The getType function has to determine the type of the triangle and prints one of the following messages:
• This is a scalene triangle!
• This is an isosceles triangle!
• This is an equilateral triangle!
In the end the main function calls the area function to calculate and print the area of the triangle.
Hint: To calculates the area s of a triangle use the following formula:

s = (p x(p-a) x (p-b) x (p-c))1/2, where p = (a + b + c)/2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// It uses three value returning functions
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
// function that will check if values are a triangle.
bool isTriangle (double, double, double);
// function that will specify what the triangle is.
void getType (double, double, double);
// function will get area of the triangle.
double getArea (double, double, double);
double piece (double, double, double);

int main()
{
double a, b, c, s;
double tR;
double piece;

cout << "Enter three sides of a triangle." << endl;
cin >> a >> b >> c;

tR = isTriangle (a, b, c);
cout << "These sides represent your triangle." << endl;
}
bool isTriangle (double a, double b, double c)
{
	if ( a < 0 && b < 0 && c < 0)
	return false;

	else
	return  true;
}
void getType (double a, double b, double c)
{
	if (a == b && b == c)
		cout << "This is an equilateral triangle!" << endl;
	
	else if (a == b || b == c || a == c)
		cout << "This is an isosceles triangle!" << endl;

	else
		cout <<"This is a scalene triangle!" << endl;
}
double piece (double a, double b, double c)
{
return (a + b + c)/2;
}
double getArea (double a, double b, double c)
{
	return (piece(a,b,c)*(piece(a,b,c) - a)*(piece(a,b,c) - b)*(piece(a,b,c)-c))* 1.5;
}


I know I'm way off. I just wanted to see if I could get one set of numbers in there. The problem I'm having is making sets of numbers. He saying that there should be 5 sets of 3 numbers. Should I make them strings? Or should it be a switch? I'm lost when it comes to that point. Any input?

Last edited on
Once again, any help would be appreciated. I don't know if I need to make a switch somewhere for the 5 sets of 3 numbers.. or maybe a string. I don't know.
Actually, you aren't that far off. Your "isTriangle" function returns a bool, but you are assigning to a double.

If you want to get information on 5 triangles, use a for loop to loop the code that gets the triangle.
isTriangle() needs more logic in it.


Three numbers are sides of a triangle if all numbers are positive and the sum of
any two sides is longer than the remaining side.


You've coded the bolded part of the above, but not the rest.

Also, I'm a little confused by your professor's requirement that the lengths should be doubles, because
it isn't simple to compare doubles for equality due to floating point roundoff error. Otherwise your
getType() function looks right.
Okay, so the new code I have made is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// It uses three value returning functions
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
// function that will check if values are a triangle.
bool isTriangle (double, double, double);
// function that will specify what the triangle is.
void getType (double, double, double);
// function will get area of the triangle.
double getArea (double, double, double);
double piece (double, double, double);

int main()
{
double a, b, c, s;
double tR;
double piece;

for (tR= 1; tR<= 5; tR++)
{
cout << "Enter three sides of a triangle." << endl;
cin >> a >> b >> c;

tR = isTriangle (a, b, c);
cout << "These sides represent your triangle." << endl;
}
bool isTriangle (double a, double b, double c)
{
	if ( a < 0 && b < 0 && c < 0)
	return false;

        else if (a + b > c || c + b > a || a + c > b)
        return true;

	else
	return  true;
}
void getType (double a, double b, double c)
{
	if (a == b && b == c)
		cout << "This is an equilateral triangle!" << endl;
	
	else if (a == b || b == c || a == c)
		cout << "This is an isosceles triangle!" << endl;

	else
		cout <<"This is a scalene triangle!" << endl;
}
double piece (double a, double b, double c)
{
return (a + b + c)/2;
}
double getArea (double a, double b, double c)
{
	return (piece(a,b,c)*(piece(a,b,c) - a)*(piece(a,b,c) - b)*(piece(a,b,c)-c))* 1.5;
}


I'm not at a PC with the visual basic, so if there are errors, I am sorry.
One mistake

1
2
3
4
else if (a + b > c || c + b > a || a + c > b)
    return true;
else
    return true;   // Are you sure about this???? 

if ( a < 0 && b < 0 && c < 0)

(a + b > c || c + b > a || a + c > b)

There are problems with both those comparisons.

ANDing conditions means all conditions have to be true for the output to be true.
ORing conditions means if any one condition is true, then the output is true.

Your first condition will only be true (and thus execute the return false;) if all three values are negative.

Your second condition will be true if just one of the sum of two sides is greater than the other (which means it will always be true). For it to be a valid triangle though, all three checks have to be true.
So with those else statements, it should be else return false.

And I could make the OR statements AND? Like this:
1
2
3
4
else if (a + b > c && c + b > a && a + c > b)
    return true;
else
    return false

Topic archived. No new replies allowed.