answersLogoWhite

0


Best Answer

That is correct - In c plus plus you cannot assign integer value to enum - You can only assign an enum value to an enum.

Even though an enum looks like an integer, it is not. It is an enum, and C++ implements strict type checking to reduce the probability of bad programming practices.

enum ColorCode {black, brown, red, orange, yellow, green, blue, violet, grey, white};

ColorCode myColorCode;

myColorCode = yellow;

Even though yellow has an integer value of 4, you cannot say myColorCode = 4.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In c plus plus you cannot assign integer value to enum?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How would you write a program that read an integer for display each of digit of integer in English?

Use an enum if you are using a c style language. Or a map data structure. Assign each integer an English value and then match it to what the user inputs.


Which datatype cannot be checked in switch case?

enum


What is a enumerated data type?

An enumeration is a group of (closely) related constants of integral type (int, char, short, long or long long). Members of an enumeration are automatically initialised with values in the order they are declared, starting with the value 0 for the first member and incrementing by 1 for each additional member: enum traffic_light {green, amber, red}; // e.g., green=0, amber=1, red=2 If the start value 0 is undesirable, we can assign a different start value: enum traffic_light {green=1, amber, red}; // e.g., green=1, amber=2, red=3 A new start value can be placed anywhere in an enum: enum traffic_light {green, amber=4, red}; // e.g., green=0, amber=4, red=5 Two or more members can share the same value: enum traffic_light {green, amber, orange=1, red}; // e.g., green=0, amber=1, orange=1, red=2 The value of a previously declared member can be used to initialise another member: enum traffic_light {green, amber=green+1, red=green+2}; // e.g., green=0, amber=1, red=2 The names of an enumeration are within the scope of the declaration. This is often undesirable as two enumerations in the same scope cannot share the same member names: enum traffic_light {green, amber, red}; enum rainbow {red, orange, yellow, green, blue, indigo, violet}; // error! To avoid this, we can use an enum class. The enumeration then becomes a namespace: enum class traffic_light {green, amber, red}; enum class rainbow {red, orange, yellow, green, blue, indigo, violet}; // ok To refer to a member of an enum class, we use namespace resolution: traffic_light x = red; // error! no red in scope rainbow y = rainbow::green; // ok


Difference between enum and const in c?

members in union and enum both shares memory and the size of the union is the size of the highest element like if there is one int and one char in union then the size of union will be the size of an int. if an int and an float are there then the size of the union will be the size of the float. This is not the case in the enum. it has similar type of members. and the value of the members can be assign manually like this enum e{ZERO,ONE,TWO=20,THREE}; here ZERO will have value 0, ONE will have value 1, TWO will be having 20 , THREE will be having 21.. and so on Answer: they are no way similar, no point in comparing them.


Enum in java?

Enum in java is a keyword which is introduced in JDK 1.5 and its a type like Interface and Class.Enum constants are implicitly static and final and you can not change there value once created. Enum in Java provides type-safety and can be used inside switch statment like int variables. Since enum is a keyword you can not use as variable name and since its only introduced in JDK 1.5 all your previous code which has enum as variable name will not work and needs to be refactored.

Related questions

How would you write a program that read an integer for display each of digit of integer in English?

Use an enum if you are using a c style language. Or a map data structure. Assign each integer an English value and then match it to what the user inputs.


Why is an enumeration?

An alternative method for naming integer constants is often more convenient than 'const' . This can be achieved by using keyword enum. for example : enum { START , PAUSE , GOO };


What is enum in c sharp?

Enum in C#, most of time, are used to defined integer-based values in symbolic ways.Example - exiting or return codesTraditionally, when a program exits, it may return an integer value to indicate the status of the execution.0 everything is OK1 Something was wrong, but not fatal2 Fatal Errorpublic enum ExitCode {OK = 0, Warning = 1, FatalError = 2}... return ExitCode.OK; // old way would by return 0; and one would need to know 0 stands for


Which datatype cannot be checked in switch case?

enum


What is a enumerated data type?

An enumeration is a group of (closely) related constants of integral type (int, char, short, long or long long). Members of an enumeration are automatically initialised with values in the order they are declared, starting with the value 0 for the first member and incrementing by 1 for each additional member: enum traffic_light {green, amber, red}; // e.g., green=0, amber=1, red=2 If the start value 0 is undesirable, we can assign a different start value: enum traffic_light {green=1, amber, red}; // e.g., green=1, amber=2, red=3 A new start value can be placed anywhere in an enum: enum traffic_light {green, amber=4, red}; // e.g., green=0, amber=4, red=5 Two or more members can share the same value: enum traffic_light {green, amber, orange=1, red}; // e.g., green=0, amber=1, orange=1, red=2 The value of a previously declared member can be used to initialise another member: enum traffic_light {green, amber=green+1, red=green+2}; // e.g., green=0, amber=1, red=2 The names of an enumeration are within the scope of the declaration. This is often undesirable as two enumerations in the same scope cannot share the same member names: enum traffic_light {green, amber, red}; enum rainbow {red, orange, yellow, green, blue, indigo, violet}; // error! To avoid this, we can use an enum class. The enumeration then becomes a namespace: enum class traffic_light {green, amber, red}; enum class rainbow {red, orange, yellow, green, blue, indigo, violet}; // ok To refer to a member of an enum class, we use namespace resolution: traffic_light x = red; // error! no red in scope rainbow y = rainbow::green; // ok


Difference between enum and const in c?

members in union and enum both shares memory and the size of the union is the size of the highest element like if there is one int and one char in union then the size of union will be the size of an int. if an int and an float are there then the size of the union will be the size of the float. This is not the case in the enum. it has similar type of members. and the value of the members can be assign manually like this enum e{ZERO,ONE,TWO=20,THREE}; here ZERO will have value 0, ONE will have value 1, TWO will be having 20 , THREE will be having 21.. and so on Answer: they are no way similar, no point in comparing them.


What is Enum and why use in your program?

The enum keyword provides a handy way to create a sequence of integer constants in a concise manner. Optionally the declaration can include a name for the sequence after the enum keyword. The constant names follow with a comma and placed within braces. The name assigned can be used to call the function again at later stage. The constants can be assigned any individual value but the following constant will always increment it by one.


Why do you have enumeration?

There are different methods for naming a integer constant, one such method is enumeration. Enumeration is created by using the keyword enum.


Enum in java?

Enum in java is a keyword which is introduced in JDK 1.5 and its a type like Interface and Class.Enum constants are implicitly static and final and you can not change there value once created. Enum in Java provides type-safety and can be used inside switch statment like int variables. Since enum is a keyword you can not use as variable name and since its only introduced in JDK 1.5 all your previous code which has enum as variable name will not work and needs to be refactored.


What is difference between C enum and C plus plus enum?

In C++, enum signifies a slightly stronger type than in C. For example, in C, one could write: enum Direction { UP, DOWN }; Direction d = 1; In C++, this would be illegal, only UP or DOWN can be assigned to a variable of type Direction, though there is still implicit casting to integer, so one could still write: int i = UP; Another difference has to do with the way variable are declared in general in C++. In C, once the enum was declared as above, declaring variables of type Direction would have to be done through the enum keyword, like this: enum Direction d = UP; In C++, the name "Direction" becomes a type in itself, so you can write: Direction d = UP;


What is meant by enum in C programming?

The enum keyword means enumeration.


Why enum data type cannot be checked in switch case?

It should work... perhaps you made some mistakes.