add to enum items

Oct 14, 2015 at 6:11pm
enum Ship
{
SHIP_BATTLESHIP,
SHIP_AIRCRAFT_CARRIER,
SHIP_CARGO_SHIP,
SHIP_DESTROYER,
SHIP_BARGE,
SHIP_TUGBOAT,
};

How do I add SHIP_TITANIC to the list? I have tried static cast and it doesnt work/am not doing it right:

Oct 14, 2015 at 6:41pm
1
2
3
4
5
6
7
8
9
10
enum Ship
{
  SHIP_BATTLESHIP,
  SHIP_AIRCRAFT_CARRIER,
  SHIP_CARGO_SHIP,
  SHIP_DESTROYER,
  SHIP_BARGE,
  SHIP_TUGBOAT,
  SHIP_TITANIC
};


You can't change enums at runtime. Are you looking for a container of sorts?
Oct 14, 2015 at 7:43pm
Not quite. Let me restate the question.

Here is an enum list:

enum Color
{
COLOR_BLACK, // assigned 0
COLOR_RED, // assigned 1
COLOR_BLUE, // assigned 2
COLOR_GREEN, // assigned 3
COLOR_WHITE, // assigned 4
COLOR_CYAN, // assigned 5
COLOR_YELLOW, // assigned 6
COLOR_MAGENTA // assigned 7
};

Color color = static_cast<Color>(5); // ugly


What does this statement do? 5 of what? What equals 5?
Oct 14, 2015 at 9:47pm
What does this statement do? 5 of what? What equals 5?


Color color = COLOR_CYAN;
Last edited on Oct 14, 2015 at 9:48pm
Oct 15, 2015 at 2:24pm
Color color = COLOR_CYAN;

but we already knew that from our list right?
Oct 15, 2015 at 2:34pm
Um... no. Your so-called "list" doesn't say anything about a variable called color.

Your enum statement is defining a type called Color.

This:

Color color = static_cast<Color>(5); // ugly

declares a variable called color of that type, and initialises its value to COLOR_CYAN.

This:

Color color = COLOR_CYAN;

does exactly the same thing.

EDIT: A little part of me dies inside every time I have to use the American spelling of "colour" :P
Last edited on Oct 15, 2015 at 2:35pm
Oct 15, 2015 at 3:40pm
Oh. Got it. : ) Thx
Oct 16, 2015 at 3:10pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum SHIPS
{
	SHIP_BATTLESHIP = -5,
	SHIP_AIRCRAFT_CARRIER,
	SHIP_CARGO_SHIP = 100,
	SHIP_DESTROYER,
	SHIP_BARGE,
	SHIP_TUGBOAT
};
...
int inputShip;
	cin >> inputShip;
	ship = static_cast<SHIPS>(inputShip);
	cout<<ship;
	SHIP_BATTLESHIP = ship;
	return 0;


error code:\src\main.cpp:48:18: error: lvalue required as left operand of assignment

I thought I handled it correctly (assigning a user nput to be assigned to an enumerated type)
Oct 16, 2015 at 3:13pm
SHIP_BATTLESHIP = ship; What are you trying to do here? This statment is equivalent to -5 = ship;
Oct 16, 2015 at 3:16pm
SHIP_BATTLESHIP = ship;

changed to this:

SHIPS SHIP_BATTLESHIP = ship

I am trying to allow the user to input and change the enum value.
Last edited on Oct 16, 2015 at 3:17pm
Oct 16, 2015 at 3:23pm
You can't do that. That's like asking the user to enter a new value for the number "-5".

In your enum definition, you're telling the compiler that the symbol SHIP_BATTLESHIP means "-5". You can't change that at runtime.

Why would you even want the user to be able to change that value? What purpose would it serve?
Last edited on Oct 16, 2015 at 3:23pm
Oct 16, 2015 at 3:30pm
You can't change that at runtime.


Thank you now it makes sense to me. I was trying to write a run time routine; I didn't know that you couldn't.

Why would you even want the user to be able to change that value? What purpose would it serve?


It wouldn't -- I have a better handle on this subject matter now even though I was trying to achieve the impossible. : )
Topic archived. No new replies allowed.