another question about enum~

This is a discussion on another question about enum~ within the C++ Programming forums, part of the General Programming Boards category; Hi, folks~ what about an enum with a for loop ? I write code like this: enum pet{dog,cat,bird,crocodile}; for(i=dog;i<crocodile;i++) { ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy another question about enum~

    Hi, folks~

    what about an enum with a for loop ?

    I write code like this:

    enum pet{dog,cat,bird,crocodile};

    for(i=dog;i<crocodile;i++)
    {
    do something......;
    }

    but it didnt work, how could I get it correct ???

    thanx in advance~
    Never end on learning~

  2. #2
    Unregistered
    Guest
    This works for me :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    enum pet{dog,cat,bird,crocodile};
    main()
    {
       for(int i=dog;i<=crocodile;i++)
       {
          printf("%04d\n\r", i);
       }
       system("Pause");
    }
    The output is :
    0000
    0001
    0002
    0003

    I am using Borland C++ 5.02.

    If this is not working try :
    enum pet{dog=0,cat,bird,crocodile};

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    Of course you can. Basically an enum is a special type of integer container. That is, you couldn't do: enum fraction { half = 0.5, ...etc}. But yes, they are especially useful for loops...
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You might have forgot int i; in your code?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy

    Originally posted by Magos
    You might have forgot int i; in your code?
    the entire code is right below:

    #include <iostream>
    #include <string>

    void main()
    {
    enum pet{dog,cat,bird,crocodile};
    pet myPet;
    for(i=dog;i<=crocodile;i++)
    {
    cout << myPet(i) << endl;
    }
    }

    it seems nothing is of lack but couldnt work yet...............
    Is there any special rules about enum ?
    Never end on learning~

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Like he said. int i; is missing. It reamins undefined, and you're getting into some trouble with your cout statement.

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Sean
    Like he said. int i; is missing. It reamins undefined, and you're getting into some trouble with your cout statement.
    I fixed my code as follows, but no correct response yet.

    #include <iostream>
    #include <string>

    void main()
    {
    enum pet{dog,cat,bird,crocodile};
    pet myPet;
    int i;
    for(i=dog;i<=crocodile;i++)
    {
    cout << myPet(i) << endl;
    }
    }
    Never end on learning~

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    No no no. Start over! Think of an enum as an 'alias' for an int. Nothing more. And you will not be able to print the enumerations automatically by name! Look:


    enum pet { dog, cat, crocodile };

    Now, by default, the enum member 'dog' is an alias for the integer '0'. 'cat' is '1'. 'crocodile' is '2'. You can alter this default behavior, but you must experiment with it yourself. ie:

    enum pet { dog = 2, cat = 4, crocodile = 8 };





    So anyway, if you want to print the verbal representation of enumerated values, you must do it by hand.




    Code:
    
    int main()
    {
     enum pet { dog, cat, crocodile };
      
     int i;
     
     for(i = dog; i <= crocodile; i++)
     {
       switch(i)
       {
         case dog: cout << "dog" << endl;
         getch();
         break;
         case cat:  cout << "cat" << endl;
         getch();
         break;
         case crocodile: cout << "crocodile" << endl;
         getch();
         break;  
       }
     }
    
      
      return 0;
    }



    Of course, in the real world, you might use a corresponding string table...



    Code:
    
    int main()
    {
     enum pet { dog, cat, crocodile };
     
     char *look[] = {"dog", "cat", "crocodile"};
    
     int i;
     
     for(i = dog; i <= crocodile; i++)
     {
       cout << look[i] << endl;
       getch();
     }
    
    
    return 0;
    }
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Sebastiani
    No no no...

    Code:
    int main()...
    Of course, in the real world, you might use a corresponding string table...

    Code:
    int main()...
    sorry, I tested both your codes, but they couldnt work either.
    my soft is DevC++. has my compiler any limitation when compiling ?
    Never end on learning~

  10. #10
    Unregistered
    Guest
    In C enum's are integers, but not in C++. Most compilers do the casting for you, but maybe DevC++ does not. Try casting the enum to int yourself :
    Code:
    int main()
    {
     enum pet { dog = 0, cat, crocodile };
    
     char *look[] = {"dog", "cat", "crocodile"};
    
     int i;
    
     for(i = (int)dog; i <= (int)crocodile; i++)
     {
       cout << look[i] << endl;
       getch();
     }
    return 0;
    }
    It will also help if you can give the error messages you receive when trying to compile.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Which compiler does the Dev-C++ IDE use?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 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 about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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