Link to home
Start Free TrialLog in
Avatar of nickmcdonald
nickmcdonald

asked on

How do I pass an Enumeration type in a function argument, where enum is declare in the int main() function.

I have a question about how to pass an Enumeration type in a function argument, can some one help me please...

The following is my code:

______________________________________________________
#include <iostream>

using namespace std;

void printColor(int[], int, enum);

const int MAX=5;


int main ()

{
  enum colors {RED, YELLOW, GREEN, BLUE, PINK};

  colors myColor;
  int colorChoice[MAX], choice=-1;

 
  cout <<"Please tell me your fav. color\n"
  cout <<"Enter 0 for RED, 1 for YELLOW, 2 for GREEN, 3 for BLUE
  and 5 for PINK, 6 to Exit\n"
  cin >> choice;

  while (choice!=6)
  {
     mycolor=colors(choice);
     colorChoice[mycolor]=+1;
     cout <<"Please tell me your fav. color\n"
     cout <<"Enter 0 for RED, 1 for YELLOW, 2 for GREEN, 3 for BLUE
     and 5 for PINK, 6 to Exit\n"
     cin >> choice;

  }
   printColor(colorChoice, MAX, myColor);

 return 0;

}

void printColor(int colorSelected[], int numOfColors, colors myColor)
{
    int index;
    for (index=0; index<=numOfColors, numOfColors++)
    {
       switch (index)
       {
         case: RED
               cout<<"RED color seleted"
                     <<colorSeleted[index]<<endl;
         case: YELLOW
             cout<<"YELLOW color seleted"
                             <<colorSeleted[index]<<endl;
         case: GREEN
               cout<<"GREEN color seleted"
                     <<colorSeleted[index]<<endl;
         case: BLUE
             cout<<"BLUE color seleted"
                             <<colorSeleted[index]<<endl;
         case: PINK
             cout<<"PINK color seleted"
                             <<colorSeleted[index]<<endl;
         default:
                        cout <<"Invalid color"<<endl;
        }
   }
}


Why I can't pass the enum in my function header?
Using VC++ and compiler giving errors
I know I can make the enum colors globle and it will work fine
But the idea here to define it in the main () block and then pass it as an argument in printColor function.
Is it at all possible in C++ to declare enum type in the main function and then pass it as an argument in function?
If so please tell me how I can change the above code such as function prototype and header so that it will work fine.

PLEASE HELP ME OUT...

THNNKS
ASKER CERTIFIED SOLUTION
Avatar of bcladd
bcladd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nickmcdonald
nickmcdonald

ASKER

So it's absolutely impossible to declare an enumeration type in the int main() and then pass it as a argument in the fucntion. If I need to pass enumeration in the function argument then it has to be declare always in the Globle area.

I understand thats what you mean right. If so could you please explain.... why I can't pass enumeration type in the function argument like int or char.

In other words I can declare an int variable local in the main() function and pass it in a function argument that is called from the main(). Why can't I do this with enumation type same way.

Also  in c++ switch statement default case should have a break or not like rest of the case statement. Please explain if yes why  or if no why..?

Thanks

Thanks
Scope is the part of the program where a declaration is visible. If you declare a variable inside of a block of code, that variable is only visible within that block. So if I  have the following:

int main() {
    int superDuper = 99;
    //...
} // end of main

int f() {

} // end of f

I cannot use superDuper inside of f because it is not "in scope" or visible after the closing brace of the block where superDuper was declared.

Type names are similarly scoped and declaring an enum creates a type name. Thus colors only exists inside of main() in your example.

Declaring a TYPE globally is typically considered acceptable software engineering (where global VARIABLES are typically frowned upon).

As to break, I tend to put break in all of the time (including default) though there is no difference in meaning to having break in the last case and letting the case fall through to the end of the switch. There may be a slight difference in the generated machine code but the effect should be identical.

-bcl
Please tell me the following code is correct or not

int f(int);

int main() {
    int superDuper = 99;
    int result=0;
   
     result =f(superDuper);
     cout <<reult<<endl; // so reault will be 9900.
    //...
} // end of main

int f(int test) {

   int final = test*100;
   return final;
} // end of f


If my above program is valid and work why can't I do the following, please explain...

void f(enum);

int main() {
    enum daysofWeek {MON, TUE, WED, THU,  FRI, SAT, SUN}
    daysofWeek day;
    int result=0;
   
     result =f(day);
     cout <<reult<<endl; // so reault will be 9900.
    //...
} // end of main

void f(daysofWeek myday) {

   int index=1;
   myday=daysofWeek(index);
} // end of f


Thanks a lot for your help
Can someone explain me about the the following code

int f(int);

int main() {
    int superDuper = 99;
    int result=0;
   
     result =f(superDuper);
     cout <<reult<<endl; // so reault will be 9900.
    //...
} // end of main

int f(int test) {

   int final = test*100;
   return final;
} // end of f


If my above program is valid and work why can't I do the following, please explain...

void f(enum);

int main() {
    enum daysofWeek {MON, TUE, WED, THU,  FRI, SAT, SUN}
    daysofWeek day;
    int result=0;
   
     result =f(day);
     cout <<reult<<endl; // so reault will be 9900.
    //...
} // end of main

void f(daysofWeek myday) {

   int index=1;
   myday=daysofWeek(index);
} // end of f


enum is NOT a type name. A function prototype MUST have a type name for each parameter. As I explained before, the type is the name of the enumeration you create (daysofWeek in this example). Again: enum is NOT a type; it is a keyword for constructing a type. Similarly you cannot declare a parameter (or variable) to be type struct.

int IS a type (a built-in type) so the first example works.

-bcl