GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Oct-2007, 17:50
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Looping with enum


CPP / C++ / C Code:
enum myenum{a,b,c,d,e};

void test(myenum temp)
{
   ...
}

void main()
{
   //how do I use a loop to call test and send it each enum value?
}
  #2  
Old 01-Oct-2007, 21:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Looping with enum


Quote:
Originally Posted by Spidy08
CPP / C++ / C Code:
enum myenum{a,b,c,d,e};
... loop to c...send it each enum value?
}

CPP / C++ / C Code:
#include <iostream>

using namespace std;

enum myenum{a,b,c,d,e};

void test(enum myenum temp)
{
    printf("   In test: ");
    switch (temp)
    {
        case a: cout << "temp == a" << endl;
                break;

        case b: cout << "temp == b" << endl;
                break;

        case c: cout << "temp == c" << endl;
                break;

        case d: cout << "temp == d" << endl;
                break;

        case e: cout << "temp == e" << endl;
                break;

        default: cout << "Grievous program error, argument to test == "
                 << temp
                 << endl;
    }
    cout << endl;
}


int main()
{
    myenum i;
    for (i = a; i <= e; i = myenum(i+1)) {
        cout << "In main(): calling test(" << i << ")" << endl;
        test(i);
    }
    cout << endl;
    cout << "After loop: calling test(" << i << ")" << endl;
    test(i);
    return 0;
}

Output:
Code:
In main(): calling test(0) In test: temp == a In main(): calling test(1) In test: temp == b In main(): calling test(2) In test: temp == c In main(): calling test(3) In test: temp == d In main(): calling test(4) In test: temp == e After loop: calling test(5) In test: Grievous program error, argument to test == 5

Regards,

Dave
  #3  
Old 01-Oct-2007, 23:27
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 115
ahbi82 will become famous soon enough

Re: Looping with enum


In addition to what davekw7x have shown, below is another example on the looping.

CPP / C++ / C Code:
typedef enum 
{
   test_1 = 1, 
   test_2 = 2, 
   test_3 = 3, 
   test_End

} testENUM;

int main ( void )
{
   int i = 0;

   for( i = test_1; i < test_End; i++ )
   {
      printf( "i = %d\n", i );

   }

   return 0;
}

Hope this helps!!!

  #4  
Old 02-Oct-2007, 07:32
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Re: Looping with enum


Thanks a lot Dave..

I was trying to do the i++, didn't think of casting the i+1 to myenum type.

)
  #5  
Old 02-Oct-2007, 09:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Looping with enum


Quote:
Originally Posted by Spidy08
I was trying to do the i++, didn't think of casting the i+1

This highlights some of the differences between enums in C and enums in C++. See footnote.

In C, enums are merely a notational convenience for named integers. (That is, an enum is actually an int data type.)

In C++ enums are their own data type with a few interesting features:

1. There are no '+' or '++' or other arithmetic operators for enums in C++ (So, if i is an enum data type you can't simply say i++.)

2. In arithmetic expressions involving ints and enum data types, the enum data variables are "promoted" to ints, so the expression i+1 promotes i to an int and adds 1. The result is an int.

3. In C++ you can't assign an integer value to an enum variable without a cast. I used function-style cast, but you could use a C-style cast or a C++ static cast.


Regards,

Dave

Footnote:
In C, if i is your enum data type you could simply use
CPP / C++ / C Code:
for(i = a; i <= e; i++) {
and no casting would be required.
Last edited by davekw7x : 02-Oct-2007 at 10:10.
  #6  
Old 02-Oct-2007, 10:44
davis
 
Posts: n/a

Re: Looping with enum


Quote:
Originally Posted by davekw7x
This highlights some of the differences between enums in C and enums in C++.

...yet another reason why C++ is not a superset of C.


:davis:
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Looping Question Gamer_2k4 C++ Forum 4 06-Nov-2006 17:23
Using Enum , and more card stuff Rob45 C++ Forum 1 24-May-2006 21:14
Need help with Looping Harryt123 C Programming Language 4 21-May-2006 15:45
ask looping likeit C Programming Language 4 26-Jul-2005 20:49
Pulldown menu with data from 2 tables or 1 with an ENUM choice? mytwocents MySQL / PHP Forum 1 10-Aug-2004 17:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.