programming with "enum"

This is a discussion on programming with "enum" within the C++ Programming forums, part of the General Programming Boards category; Hey... I've been working on this program using the enumeration type. When I execute what I've written, it returns a ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question programming with "enum"

    Hey... I've been working on this program using the enumeration type. When I execute what I've written, it returns a number instead of the type of triangle it is. If you could look at the code and help me out I'd appreciate it!

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum triangleType {scalene, isosceles, equilateral, noTriangle};
    
    triangleType triangleShape(double, double, double);
    
    int main()
    {
    	double firstSide, secondSide, thirdSide;
    	
    	cout << "Enter the lengths of the three sides of a triangle." << endl;
    	cin >> firstSide >> secondSide >> thirdSide;
    	cout << endl;
    
    	cout << "The shape of the triangle is: "
    		 << triangleShape(firstSide, secondSide, thirdSide) << endl;
    
    	return 0;
    }
    triangleType triangleShape(double firstSide, double secondSide, double thirdSide)
    {
    	triangleType type;
    
    	if (firstSide + secondSide <= thirdSide || firstSide + thirdSide <= secondSide ||
    		secondSide + thirdSide <= firstSide)
    	{
    		type = noTriangle;
    	}
    	if (firstSide + secondSide > thirdSide && firstSide + thirdSide > secondSide &&
    		secondSide + thirdSide > firstSide)
    	{
    		if (firstSide != secondSide && firstSide != thirdSide && secondSide != thirdSide)
    		{
    			type = scalene;
    		}
    		else if (firstSide == secondSide || firstSide == thirdSide || secondSide == thirdSide)
    		{
    			type = isosceles;
    		}
    		else
    		{
    			type = equilateral;
    		}
    	}
    	return type;
    }

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    enum's are enumerated constants. They represent numbers. The first entry in an enum statement represents 0, the second 1 and so on.

    You can of course change this:

    Code:
    enum triangles { equilateral=10, scalene=20 }; // etc
    If this isn't what you meant, well, sorry.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    No, that's not what I meant but thanks for tryin. In my function I'm trying to return the type of triangle after the user has input the lengths of the three sides of a triangle. But when I run the program, it's giving me those values; 0, 1, 2, etc. Any one got some ideas for me?!?!

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    When the user inputs the # sides for a scalene triangle, what number is output?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    well yeah.. it is suppose to return a number..
    Code:
    enum triangleType {scalene, isosceles, equilateral, noTriangle};
    //scalene = 0;
    // isosceles = 1;
    // equilateral = 2;
    //noTriangle = 3;
    and the function is going to return one of those values.

    if you want it return a string like "Equilateral" make a string function

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    There's your answer - the same as my reply
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by ahluka
    There's your answer - the same as my reply
    sorry i didn't see your reply.. you must of posted it seconds before mine..

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Well you replied to my reply saying thanks for trying. Never mind about it, at least you won't have problems with it again

    *edit*

    Damn, sorry, I thought you were the one who started the thread lol. Yeah I posted it about the same time
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by mrafcho001
    well yeah.. it is suppose to return a number..
    Code:
    enum triangleType {scalene, isosceles, equilateral, noTriangle};
    //scalene = 0;
    // isosceles = 1;
    // equilateral = 2;
    //noTriangle = 3;
    and the function is going to return one of those values.

    if you want it return a string like "Equilateral" make a string function
    But for an enumeration type, aren't "scalene, isosceles, etc" variables, not strings? How would I set that up if making it return a string is the only way I can do it?

  10. #10
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    first start off with making the function return a string:
    Code:
    #include <iostream>
    using namespace std;
    
    string Triangle(double side1, double side2, double side3)
    {
        //Here check the sides like you did on the other function
       //and return the right string for examlpe for Equilateral triangle "Equilateral"
    
    }
    
    int main()
    {
    
    return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,821
    Well, you could do something like this just after the enum:
    Code:
    std::string triangleName[] = {"scalene", "isosceles", "equilateral", "noTriangle"};
    After which triangleName[isosceles] would be the string "isosceles".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    The point of my program though is on using the enumeration type. Shouldn't there be another way for me to return what I want it to?

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    string trianglePrint (triangleType t) {
       switch(t) 
       {
          case scalene: return "scalene";
          case isosceles: return "isosceles";
          case equilateral: return "equilateral";
          case noTriangle: return "No Triangle";
          default: return "Error";
       }
    }
    Or you can directly print it...
    Code:
    void trianglePrint (triangleType t) {
       switch(t) 
       {
          case scalene: cout << "scalene"; break;
          case isosceles: cout << "isosceles"; break;
          case equilateral: cout << "equilateral"; break;
          case noTriangle: cout << "No Triangle"; break;
          default: cout << "Error";
       }
    }

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I'm supposed to be able to return the shape of the triangle from the triangleShape function.

  15. #15
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You're not making much sense here. Do you want to return a string indicating which shape it is (the examples fellow repliers have posted above) or, like, a vertex for use in '3d space'.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21