Bool functions within an Array

Hello,

I'm not exactly sure if my code is correct in the bool 'isTriangle' (Lines 83-94) function. I want to be able to keep the user from going any further in the program if their input does not meet the qualifications for a triangle.

Also, how can I print the results on the screen that the lengths entered by the user do not form a triangle (if that is the case)? Do I do this in the main() function?

Thanks Guys,
Sarah

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <string>
using namespace std;

//Function prototypes
void getLengths(double[], double);
void sortArray(double[], double);
bool isTriangle(double[], double);
void equalSides(double[], double);
void angleType(double[], double);




int main()
{
	const int sides = 3;
	double triangleLengths[sides];
	
	//Get three lengths of triangle from user
	getLengths(triangleLengths, sides);

	//Sort Array in ascending order
	sortArray(triangleLengths, sides);

	//See if entered side lengths can form a triangle
	isTriangle(triangleLengths, sides);

	//Determine whether the triangle is equilateral, isosceles, or scalene
	equalSides(triangleLengths, sides);

	//Determine whether the triangle is right, acute, or obtuse
	angleType(triangleLengths, sides);


	return 0;

}

void getLengths(double lengths[], double sides)
{
	//Loop counter
	int index;

	//Get 3 sides
	for (index = 0; index < sides; index++)
	{
		cout << "Enter the length of side " << (index + 1) << ": ";
		cin >> lengths[index];
		{
			while (lengths[index] <= 0)
			{
				cout << "Please enter a length greater than zero " << endl;
				cin >> lengths[index];
			}

		}
	}
}


void sortArray(double array[], double sides)
{
	bool swap;
	double temp;

	do
	{
		swap = false;
		for (int count = 0; count < (sides - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
}

bool isTriangle(double lengths[], double sides)
{
	int i = 0;
	
		if (lengths[i] + lengths[i + 1] > lengths[i + 2] ||
			lengths[i] + lengths[i + 2] > lengths[i + 1] ||
			lengths[i + 1] + lengths[i + 2] > lengths[i])
			return true;
		else
			return false;
	
}

void equalSides(double lengths[], double sides)
{

	int i = 0;

		if (lengths[i] == lengths[i + 1] && lengths[i] == lengths[i + 2])
			cout << "The triangle is equilateral" << endl;
		else if (lengths[i] == lengths[i + 1] || lengths[i] == lengths[i + 2])
			cout << "The triangle is isosceles" << endl;
		else
			cout << "The triangle is scalene" << endl;

}

void angleType(double lengths[], double sides)
{

	int i = 0;

		if (lengths[i + 2] * lengths[i + 2] == lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
			cout << "The triangle is also a right triangle" << endl;
		else if (lengths[i + 2] * lengths[i + 2] > lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
			cout << "The triangle is also obtuse" << endl;
		else if (lengths[i + 2] * lengths[i + 2] < lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
			cout << "The triangle is also acute" << endl;

}


I'm not exactly sure if my code is correct in the bool 'isTriangle' (Lines 83-94) function.

It's not. All 3 conditions must hold. Yours returns true if any one of the conditions holds.

Do I do this in the main() function?

Yes. Your isTriangle function returns a value. Make use of it.
Topic archived. No new replies allowed.