Elements-Check??

Started by
5 comments, last by AngelForce 21 years, 5 months ago
Ok, is there a way to do something like this?
  
typedef enum Letter_A
{
A,a
};


bool letter_a(char ch)
{
if(ch IS ELEMENT OF Letter_A)
{
return true;
}
return false;
}
  
Or would I have to check every element( if(ch == ''A'' || ch == ''a) ) ??? Thanks for any replies! AngelForce -- If you find any mistakes, you''re allowed to keep ''em! ''When I look back I am lost.''
AngelForce--"When I look back I am lost." - Daenerys Targaryen
Advertisement
What''s wrong with:

  bool is_letter_a(char ch){  return (ch == ''a'' || ch == ''A'');}  // or even...char upcase(char ch){   return (''a'' <= ch && ch <= ''z'') ? (ch - ''a'' + ''A'') : ch;}     bool is_same_letter_ignoring_case(char ch1, char ch2){  return upcase(ch1) == upcase(ch2);}  //testbool check = is_same_letter_ignoring_case(ch, ''a'');  

enums are types. Treat ''em as such.


  enum some_enum{  elem1,  elem2,  elem3,  elem4};void some_function(some_enum a){ // blah}// ...some_enum arg = elem2;some_function(arg);// ...  
daerid@gmail.com
If your problem is mainly about character handling, use the toupper and tolower functions to convert a character so that you only have to compare against one case.

If your problem is about being able to find something in a set, you probably want std::set, although that requires you to use C++. (I say this because your typedef implies otherwise, even if it is pointless in the context).

std::set<char> letterA;
letterA.insert(''a'');
letterA.insert(''A'');

if (letterA.find(ch) != letterA.end()) return true; else return false;

Note that this example is convoluted, but it would save you a lot of typing (and potential errors) if the set contained a lot of elements.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
Ok, I posted a bad example
I just wanted to show the core of my question...please consider this:


    typedef enum typ1{	A = 'A',	a = 'a'};typedef enum typ2{	_A = 1,	_B = 4,	_C = 35,	_D = 156};typedef enum typ3{	__A = "text1",	__B = "text2"};typedef enum typ4{	typ1,typ2,typ3};int test(typ4 tt){	if(tt IS ELEMENT OF typ1)		return 1;	else if(tt IS ELEMENT OF typ2)		return 2;	return 3;}    

(Ok, I am not sure, if all the enums would work, but I can at least compile it )

So, how would you do such a check, when the typ can be (eg) char, int or string?
std::set sounds good, but I dunno how I would implement it in a situation like this.

Please shout very loud if you don't understand my babbling, I'll try then to formulate it better(heck, English is kind of pain =) ).

__EDIT__
I do use C++ but 'typedef enum' sounds better than 'enum'
_/EDIT__

AngelForce

--
If you find any mistakes, you're allowed to keep 'em!

'When I look back I am lost.'

[edited by - AngelForce on October 24, 2002 4:51:10 AM]
AngelForce--"When I look back I am lost." - Daenerys Targaryen
You can''t.

Well, you can... but it''s basically just testing against each element of the enum separately.

Enums are meant to allow a programmer to easily refer to distinct but related integral constants by symbolic names. Think of them as special-purpose #defines. Since they don''t actually exist in your compiled code (the compiler just replaces them with their integral value) they aren''t actually a collection you can test against.

Don''t listen to me. I''ve had too much coffee.
quote:Original post by Sneftel
You can''t.

Well, you can... but it''s basically just testing against each element of the enum separately.

Enums are meant to allow a programmer to easily refer to distinct but related integral constants by symbolic names. Think of them as special-purpose #defines. Since they don''t actually exist in your compiled code (the compiler just replaces them with their integral value) they aren''t actually a collection you can test against.

Don''t listen to me. I''ve had too much coffee.


Thanks! That''s the answer I wanted(even it''s sad that I can''t do it )
AngelForce--"When I look back I am lost." - Daenerys Targaryen

This topic is closed to new replies.

Advertisement