Function and Array Issue

Hello, I've been working on this program for my class that focuses on the use of functions and arrays. We need to make it so the user can enter 3 lengths for a triangle and the program will also tell what type of triangle it is.

I keep running into this error code and I don't understand why:

Error 1 error LNK2019: unresolved external symbol "void __cdecl isTriangle(double * const,double)" (?isTriangle@@YAXQANN@Z) referenced in function _main

Error 2 error LNK2019: unresolved external symbol "void __cdecl equalSides(double * const,double)" (?equalSides@@YAXQANN@Z) referenced in function _main

I've tried switching data types but it didn't seem to work. Assistance would be greatly appreciated!

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

// Main function calls all the other functions
int main()
{
	// Triangles always have 3 sides
	const int sides = 3;
	double triangleLengths[sides];

	// Function calls
	getLengths(triangleLengths, sides);

	sortArray(triangleLengths, sides);

	isTriangle(triangleLengths, sides);

	equalSides(triangleLengths, sides);

	angleType(triangleLengths, sides);

	system("pause");
	return 0;

}

// This function collects the three lengths from the user
void getLengths(double lengths[], double sides)
{
	// The counter for given lengths
	int index;
	// Ask user to input 3 lengths
	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];
			}

		}
	}
}

// This function sorts the array
void sortArray(double array[], double sides)
{
	bool swap;	// Swaps two double values
	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);
}

// This function confirms if the entered lengths form a triangle
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;
	}
}

// This function tells the user the type of triangle
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;
	}
}

// This function tells if the triangle is right, acute, or obtuse
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;

	}
}
Your prototypes and implementations do not agree.

8
9
void isTriangle(double[], double);
void equalSides(double[], double);


1
2
void isTriangle(const double lengths[], double sides)
void equalSides(const double lengths[], double sides)

Your function prototypes and definitions has to be the same.

1
2
void isTriangle(double[], double);
void equalSides(double[], double);


1
2
3
4
5
void isTriangle(const double lengths[], double sides)
{

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


you added const which are not there in the prototypes.
Why do you have several topics for this problem?

for example: http://www.cplusplus.com/forum/beginner/180076/
The number-of-sides passed to the function is an integer. The parameter should also be an integer.
void isTriangle(const double lengths[], double sides)
should be
void isTriangle(const double lengths[], int sides)
There are other problems also.

Line 17: triangleLengths is defined as an array of 3.

Line 41: You're going to prompt for 4 sides and attempt to store into lengths[3], which is out of bounds. Only elements 0-2 are valid.

Line 67: You're going to make an out of bounds reference if count = 2.

Line 81-87,99,101,115-119: Ditto out of bounds references.





Topic archived. No new replies allowed.