enum basic question [solved]

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I have big enum var and input from radio terminal.
Sometime is on input wrong value. I want accept only
values from enum list. I do not want check input on right value everytime in new input.
Simple example: /only for explain and debug/

uint8_t temp;
typedef enum {one=0, two = 24, three = 34} enumExampleEnum;

enumExampleEnum exampleEnum;

exampleEnum = 65;
temp = exampleEnum;

When I debug this prg part, in exampleEnum is value 65 (with note "NOT in enum") and in temp is 65 too. I expected that in enum var may be only values from list.

lx-user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I expected that in enum var may be only values from list.

But you assigned 65 to it? Ever heard of "garbage in, garbage out"? Or were you expecting a warning when you assigned the 65?

If you convert the source to C++ you get:

../foo2.cpp:9:16: error: invalid conversion from 'int' to 'enumExampleEnum'

though if you change to:

	exampleEnum = (enumExampleEnum)65;

it accepts that without warning.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Clawson,

this is only example. Not real program. 65 is wrong value, I know. I wrote: Input is from remote terminal. Sometime is wrong value on input. Simply - some user sometime enter wrong value .
Unfortunately - now I understand: enum var can has any value - not only enum values. It is my "C language mistake" I have to write function for check input values.

"C++ - important idea, but sometime I have problem with ISR routines. - it should be different question on GCC forum"

- I think, this post should be assign like "SOLVED"
lx_user