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
385 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

18 Experts available now in Live!

Get 1:1 Help Now