I've been studying enums lately and decided to make a program with it to test the enum syntax and my first one worked. It was:

#include<iostream>
using namespace std;
int main()
{
enum daysinweek{
monday=0,
tuesday=1,
};
enum daysinweek enumerator=monday;
cout<<enumerator<<endl;
system("pause");
return 0;
}

It worked perfectly, by outputting 0, like it should.
Then I tried to step it up and make the program define enumerator with a user input of 0 or 1 using a switchcase. The code was:

#include<iostream>
using namespace std;
int main()
{
	int whatday;
	int enumerator;
	cout<<"What day is it-monday or tuesday? (0 for monday, 1 for tuesday)"<<endl;
	cin>>whatday;
	enum daysinweek{
		 monday=0,
		 tuesday=1,
	};
	switch(whatday)
	{
	case 0:
		enum daysinweek enumerator=monday;		
		break;
	case 1:
		enum daysinweek enumerator=tuesday;
		break;
	}
	cout<<enumerator;
	system("pause");
	return 0;
}

But I got these error messages:
1>enum.cpp
1>.\enum.cpp(17) : error C2360: initialization of 'enumerator' is skipped by 'case' label
1> .\enum.cpp(15) : see declaration of 'enumerator'
1>.\enum.cpp(18) : error C2374: 'enumerator' : redefinition; multiple initialization
1> .\enum.cpp(15) : see declaration of 'enumerator'
1>.\enum.cpp(21) : error C2065: 'enumerator' : undeclared identifier

I want to know how i can have the user define the day while still using the enum type.

Ok. There are two problems with this.

First, you cannot declare variables within the outer scope of a switch statement. Hence lines 16 and 19 are illegal.
Second you have declared "enumerator" both at 16/19 (a local copy)
and at line 6. So you need

enum daysinweek em;
	switch(whatday)
	{
	case 0:
	  em=monday;
	  break;
	case 1:
	  em=tuesday;
	  break;
	}
	cout<<em;

I have used em, instead of enumerator, and this way em is in scope
for the cout<<em line.

Hope this helps.

commented: helpful +1

Thank you so much I knew it was probably something to do with scopes. Also, do you know of any other real uses of enum other than my example?

As a follow up, I guess that I wouldn't have used enum in your case.
It is not wrong, it is just that you normally want to convert from a constant to an internal representation. You effectively do the reverse.

The more common usage is to enumerate "magic constants".
These are easier to see with an example:

enum matrixTYPE
{
    SYMMETRIC = 1,
    DIAGONAL = 2,
    SQUARE = 4,
   ....
}

Then you use them in an

if ((mTYPE & SYMMETRIC) // instead of (mType & 1)
  {
      processHoefmanComplex();
  }

So effectively, it is making the code easier to read. Also if for various reasons you change your mind about 1, (say you merge your matrix code with another code) then you only have to change one line of code. That will avoid hours of debugging later. Code typing cost is always cheaper than debugging.

Note that often you can do similar with a const. I use const double X(value) for parameters etc. (often putting them in a global namespace. e.g namespace physics_const )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.