Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

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

what is enum and how do I use it?
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Troudeloup
Troudeloup

ASKER

can you show me a small piece of code?  (as small as possible, like, 3 values.)
#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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you all SO much !