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
nickmcdonaldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bcladdCommented:
No, it is not possible to declare the type inside a block and then use the type outside that block. The type is scoped and only exists in the block.

So, you declare the TYPE to be global and then use it as a parameter type and a local variable inside of main.

-bcl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nickmcdonaldAuthor Commented:
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
0
bcladdCommented:
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
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

nickmcdonaldAuthor Commented:
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
0
nickmcdonaldAuthor Commented:
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


0
bcladdCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.