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
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 108
How to split this in C++ 4 127
Why  my code (program) build with old compiler? 11 145
Need some help with mailto 16 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

739 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