Solved

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

Posted on 2003-11-02
6
397 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:nickmcdonald
  • 3
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 300 total points
ID: 9667897
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
 

Author Comment

by:nickmcdonald
ID: 9667961
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9668004
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nickmcdonald
ID: 9668097
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
 

Author Comment

by:nickmcdonald
ID: 9668770
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9670228
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now