Triangle Program Assistance

Hello Everyone!
In my class I was required to design a Triangle program that tells the user whether the three sides entered could form a triangle, and the program must also determine what type of triangle it is (scalene, obtuse, etc.)

I think I have got it together, except for in my angleType function, I receive an error that states "left operand must be an Ivalue" and "expression must be a modifiable Ivalue". I'm sure this is easily fixable, but I am a beginner so pleaseee take it easy on me. Also, if you don't mind, could you tell if my program is logically correct?

P.S. my function use is required.

Thanks guys!
Sarah

EDIT* I just realized I was missing some for loops in a few functions, I have corrected it.

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
126
127
128
#include <iostream>
#include <string>
using namespace std;

//Function prototypes
void getLengths(double[], double);
void sortArray(double[], double);
void 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);

	//Display results


	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);
}

void isTriangle(const double lengths[], double sides)
{
	for (int i = 0; i <= sides; i++)
	{
		if (lengths[i] + lengths[i + 1] > lengths[i + 2])
			cout << "These 3 sides form a triangle" << endl;
		else if (lengths[i] + lengths[i + 2] > lengths[i + 1])
			cout << "These 3 sides form a triangle" << endl;
		else if (lengths[i + 1] + lengths[i + 2] > lengths[i])
			cout << "These 3 sides form a triangle" << endl;
		else
			cout << "These 3 sides do not form a triangle" << endl;
	}
}

void equalSides(const double lengths[], double sides)
{
	for (int i = 0; i < 3; i++)
	{
		if (lengths[i] == lengths[i + 1] && lengths[i] == lengths[i + 2])
			cout << "This is an equalateral triangle" << endl;
		else if (lengths[i] == lengths[i + 1] || lengths[i] == lengths[i + 2])
			cout << "This triangle is isosceles" << endl;
		else
			cout << "This triangle is scalene" << endl;
	}
}

void angleType(double lengths[], double sides)
{
	
	for (int i  = 0; i <= sides; i++)
	{

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

	}
}

	
	
Last edited on
> I receive an error that states
¿line number?


= is assignment
== is comparison


> P.S. my function use is required.
¿were the prototypes imposed?
void isTriangle(double[], double); would expect that to return a bool (and the size should be an integer)


1
2
3
		for (int count = 0; count <= (sides - 1); count++) //line 70
		{
			if (array[count] > array[count + 1])
out of bounds.
The last value of `count' would be `sides-1' then you try to access array[count + 1], that is, array[sides], which is invalid.
There is no need for a general function, you only need to sort three numbers.
Thank you for your help! And I was just modifying the isTriangle to make it bool. Thanks again.
And I'm sorry if I'm not understanding this part correctly, but when you say there is no need for a general function, do you mean my sortArray function is too extensive? I know it is only three numbers, how can I make it simpler?
Topic archived. No new replies allowed.