Solved

[noob][c++] what is enum and how do I use it?

Posted on 2007-11-15
12
317 Views
Last Modified: 2009-12-16
what is enum and how do I use it?
0
Comment
Question by:Troudeloup
  • 6
  • 5
12 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 450 total points
ID: 20295524
enum is an integral data type that admit just a limited collection of values, each value has a name, asociated with a the itself.
Have a look to this tutorial:
http://www.cprogramming.com/tutorial/enum.html
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 20295783
6.7.2.2 Enumeration specifiers
Syntax
1 enum-specifier:
enum identifieropt { enumerator-list }
enum identifieropt { enumerator-list , }
enum identifier
enumerator-list:
enumerator
enumerator-list , enumerator
enumerator:
enumeration-constant
enumeration-constant = constant-expression
Constraints
2 The expression that defines the value of an enumeration constant shall be an integer
constant expression that has a value representable as an int.
Semantics
3 The identifiers in an enumerator list are declared as constants that have type int and
may appear wherever such are permitted.107) An enumerator with = defines its
enumeration constant as the value of the constant expression. If the first enumerator has
no =, the value of its enumeration constant is 0. Each subsequent enumerator with no =
defines its enumeration constant as the value of the constant expression obtained by
adding 1 to the value of the previous enumeration constant. (The use of enumerators with
= may produce enumeration constants with values that duplicate other values in the same
enumeration.) The enumerators of an enumeration are also known as its members.
4 Each enumerated type shall be compatible with char, a signed integer type, or an
unsigned integer type. The choice of type is implementation-defined,108) but shall be
capable of representing the values of all the members of the enumeration. The
enumerated type is incomplete until after the } that terminates the list of enumerator
declarations.
0
 

Author Comment

by:Troudeloup
ID: 20298540
can you show me a small piece of code?  (as small as possible, like, 3 values.)
0
 

Author Comment

by:Troudeloup
ID: 20298637
#include <iostream>
using namespace std;
 
enum e_usflcnst{
      CATLIFE_FACTOR = 7
};
 
int main()
{
      int age;
      cout << "Enter your age: ";
      cin >> age;
      age /= CATLIFE_FACTOR;
      cout << "If you were cat, you would be " << age << endl;
}







this makes sense,

but what I don't undersrtand it,  why not just use a variable for the factor?

also, what role does e_usflcnst  play here?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 450 total points
ID: 20299424
little example:

enum rainbowcolors { red, orange, yellow, green,  blue, indigo, violet  }

enum rainbowcolors trafficlights = red;
0
 

Author Comment

by:Troudeloup
ID: 20305571
i don't understand,

I get it that raidbowcolors is a enum type (just like int and double, it's a type of variable ) with elements (colors)


but then how are you using it?
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.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 450 total points
ID: 20305591
is like integers, but you cannot assign a value different that those from the list.

you will find in some typical blocks like this:

switch (trafficlights)
{
      case orange:
            cout << "careful!";
           break;
   case green:
        cout << "pass";
        break;
   case red:
       cout << "STOP";
       break;
    default:
      cout << "system malfunction!";
}


}
0
 

Author Comment

by:Troudeloup
ID: 20305613
is it like a way to use strings without having to use string variables?



so a type rainbow can have only those variants of colors , can I alter rainbow afterwards?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 450 total points
ID: 20305627
>>is it like a way to use strings without having to use string variables?
yes, it is more verbose than using 1 for orange, 2 for green, and 3 for red

>>so a type rainbow can have only those variants of colors , can I alter rainbow afterwards
no, you cannot
0
 

Author Comment

by:Troudeloup
ID: 20305634
also, I have to assign only those elements I already define in the enum type right?

for instance I can't do


rainbow trafficlights = silver



also, I don't need to quote colors?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 450 total points
ID: 20305652
yes, you can't do it

and

yes, you don't need the quotes
0
 

Author Comment

by:Troudeloup
ID: 20305656
thank you all SO much !
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

Suggested Solutions

Title # Comments Views Activity
Run Powershell Script When New File Is In A Folder 3 63
How To Loop - Python 19 79
Problem to open Excel file 15 95
Excel file not created as expected 7 46
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

919 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

16 Experts available now in Live!

Get 1:1 Help Now