Thread: Using enumerated data types

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Question 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.


  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Here is the basic syntax for using 'enums' as types:

    Code:
    enum type { type1, type2 };
    type someType = type1;
    someType = type2;
    // Etc.
    It would help if you posted some code.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    The Complete code

    Here is the complete code with comments, it is not very big:

    #include <iostream>
    using namespace std;

    enum KindOfTriangle {SCALENE, ISOSCELES, EQUILATERAL, IMPOSSIBLE};

    KindOfTriangle triangleType (double side1, double side2, double side3);

    int main()
    {
    int choice = 0;
    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))

    cout << "The type of triangle your measurements will produce is a "
    << EQUILATERAL << "."
    << endl << endl;;


    if ((side1 == side2) || (side1 == side3) || (side2 == side3))

    triangleType = ISOSCELES;


    if (((side1 + side2) < side3) || ((side1 + side3) < side2) ||
    ((side2 + side3 < side1)))

    triangleType = IMPOSSIBLE;

    else

    triangleType = SCALENE;

    return triangleType;


    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    The Complete code

    Here is the complete code with comments, it is not very big:

    #include <iostream>
    using namespace std;

    enum KindOfTriangle {SCALENE, ISOSCELES, EQUILATERAL, IMPOSSIBLE};

    KindOfTriangle triangleType (double side1, double side2, double side3);

    int main()
    {
    int choice = 0;
    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))

    cout << "The type of triangle your measurements will produce is a "
    << EQUILATERAL << "."
    << endl << endl;;


    if ((side1 == side2) || (side1 == side3) || (side2 == side3))

    triangleType = ISOSCELES;


    if (((side1 + side2) < side3) || ((side1 + side3) < side2) ||
    ((side2 + side3 < side1)))

    triangleType = IMPOSSIBLE;

    else

    triangleType = SCALENE;

    return triangleType;


    }

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    This is your code from a step back

    Code:
    void f()
    {
         f = 4;
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Thumbs up 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;
    }

    }

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What you are doing is printing the address of the function, not the output of the function. Also, when you called the function, you did not assign the returned value to anything so it was simply discarded. To get the triangle type:

    Code:
    KindOfTriangle triangle = triangleType(side1, side2, side3);
    cout << triangle;
    This will still only print a number corresponding to the various triangle types. To print a string based on the type, you can use a switch statement or if statements.

    Code:
    if(triangle == SCALENE)
        cout << "Scalene";
    // Etc...
    
    // Or
    
    switch(triangle)
    {
    case SCALENE:
        cout << "Scalene";
        break;
    case ISOSCELES:
        cout << "Isosceles";
        break;
    // Etc...
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Thumbs up 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM