Using enumerated data types
I am writing a function that will return the type of triangle generated by the length of the three sides as inputted by the user. I am using an enumerated data type that is declared as follows:
enum KindOfTriangle {SCALENE, ISOSCELES, EQUILATERAL, IMPOSSIBLE};
Next I declare a variable of this type and call it:
triangleType
Later down in the funcion I attempt to assign a value to this variable after certain condtions of an "if" statement are met with the following statement:
triangleType = ISOSCELES;
I do this for each of the four types of triangles further down in the function. When I try to compile the code I receive the following error code for each attempt to use the assignment operator:
error C2659: '=' : overloaded function as left operand
I am using Visual C++ 6.0. Does anyone have an answer for this? I know it is because I am not using the enumerated data type correctly. None of my books go into very much detail regarding this.
:confused:
I got the code to compile
OK,
Now I have gotten the code to compile. The syntax was wrong when I was attempting to use the assignment operator. Now my problem is that the answer spit out by the program is some hexadecimal address in memory like when you access an "empty" element in an array. Here is what the code looks like now. Can Anyone tell me how to get the program to print out the type of triangle that the inputted values will produce?
#include <iostream>
using namespace std;
enum KindOfTriangle {SCALENE, ISOSCELES, EQUILATERAL, IMPOSSIBLE};
KindOfTriangle triangleType (double side1, double side2, double side3);
int main()
{
double side1, side2, side3 = 0;
cout << "This program will determine if the values you enter for"
<< " the sides of a triangle"
<< endl
<< "will produce one of the following types of triangle: "
<< endl
<< "SCALENE, ISOSCELES, EQUILATERAL, or IMPOSSIBLE."
<< endl;
cout << "Enter the length of side 1: ";
cin >> side1;
cout << endl;
cout << "Enter the length of side 2: ";
cin >> side2;
cout << endl;
cout << "Enter the length of side 3: ";
cin >> side3;
cout << endl;
triangleType (side1, side2, side3);
cout << "The type of triangle your measurements will produce is a "
<< triangleType << "."
<< endl << endl;
return 0;
}
// Function: triangleType
// Task: determines the type of triangle, given the lengths of the three sides
//
// Inputs: three integers representing the lengths of the three sides
// of a triangle
// Output:
// EQUILATERAL, if all sides are equal
// ISOSCELES, if two sides are equal'
// SCALENE, if all sides are different lengths
// IMPOSSIBLE, if the integers do not consitute valid triangle sides
KindOfTriangle triangleType (double side1, double side2, double side3)
{
if ((side1 == side2) && (side2 == side3))
{
KindOfTriangle triangleType = EQUILATERAL;
return EQUILATERAL;
}
if ((side1 == side2) || (side1 == side3) || (side2 == side3))
{
KindOfTriangle triangleType = ISOSCELES;
return ISOSCELES;
}
if (((side1 + side2) < side3) || ((side1 + side3) < side2) ||
((side2 + side3 < side1)))
{
KindOfTriangle triangleType = IMPOSSIBLE;
return IMPOSSIBLE;
}
else
{
KindOfTriangle triangleType = SCALENE;
return SCALENE;
}
}
The Switch was exactly right, thanks
Thanks Zach L.,
Your advice was right on the nose. Now my algorithim could use a little work but that one I can knock out no problem. It was the syntax that was killing me. Thanks again:)