Thread: enumeration question

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    enumeration question

    How can I check for an enum type in another program. For example I have the following enumeration:

    Code:
    class Processing_Plants
    {
    public:
       enum Juice_t
        {
            Orange, 
            Grapefruit
        };
    }; // end class Processing_Plants
    I would like to be able to check to see if "Orange" or "Grapefruit" are the current state by use of an if statement and then perform some operation. My problem is this. I don't know the proper syntax for performing this check. Thanks in advance!

    JK

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    For ease lets move it out of the class and into the global scope:
    Code:
    enum Juice_t { Orange, Grapefruit };
    
    int main()
    {
      Juice_t ju = Grapefruit;
      if ( ju == Orange )
        cout << "Meh?"<<endl;
      else
        cout << "Ok this is what we expected"<<endl;
    }
    If you leave it in the class you'll have to resolve the scope with:
    Code:
    Processing_Plants::Juice_t
    and
    Code:
    Processing_Plants::Orange

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    void typeTest(Processing_Plants::Juice_t type)
    {
       if(type == Processing_Plants::Orange)
    	  cout << "It's Orange" << std::endl;
       else if(type == Processing_Plants::Grapefruit)
    	  cout << "It's Grapefruit - Yummy!" << std::endl;
    }
    **EDIT**
    Beaten.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    Very nice explanation. Thank you !

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    ooops! How would I declare and call this function?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I was able to figure out how to declare it but I can't figure our the correct syntax use for calling it.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    here is the funtion:

    Code:
    void Citrus::typeTest(Processing_Plants::Juice_t type)
    {
       if(type == Processing_Plants::Orange)
    	  cout << "It's Orange" << std::endl;
       else if(type == Processing_Plants::Grapefruit)
    	  cout << "It's Grapefruit - Yummy!" << std::endl;
    }

    What is the proper syntax for calling this function? Thanks in Advance!

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well lets look at a near example:
    Code:
    void typeTest(int i)
    {
      if ( i==1 )
        cout<<"Its a 1"<<endl;
      else if (i==2)
        cout<<"Its a 2"<<endl;
    }
    
    void somefunc()
    {
      int x = 1;
      typeTest(x);
    }
    I bolded for a reason.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I understand the passing of an "int" type into the function into the function you have provided. What is not clear to me is passing an "enum" type as shown in the function I have provided. Please show a sample of how this is accomplished. Thanks in advance!
    JK

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's exactly the same. Replace 'int' with 'Juice_t', '1' with 'Orange' and '2' with 'Grapefruit'.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just change the types from int to the enum type (which you've already done in your function). Change the constant int values to the enum values (again which you've already done in your function). Enums are just values and can be passed around as such.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    My call is hosed! Please see my code below that sjows the call to the function and the function itself.


    Code:
    void somefunc()
    {
       typeTest(Juice_t);
    }
    
    
    void Citrus::typeTest(Processing_Plants::Juice_t type)
    {
       if(type == Processing_Plants::Orange)
    	  cout << "It's Orange" << std::endl;
       else if(type == Processing_Plants::Grapefruit)
    	  cout << "It's Grapefruit - Yummy!" << std::endl;
    }

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    void somefunc()
    {
       typeTest(Juice_t);
    }
    You must pass an actual value, not the type.
    Code:
    void somefunc()
    {
       typeTest(Processing_Plants::Orange);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I appreciate your patience! I really do. If there are two possibilities (ie - Orange or Grapefruit), isn't what you suggested 'hard coding' the value being passed?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yeah, but this call is just an example anyway. How you come up with the initial value to pass (Orange or Grapefruit or Apple) is up to you.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM
  5. Enumeration question?
    By cheesehead in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2001, 06:31 PM