enum

hey ive looked on the net and other places but cant find a way to do this so i thought ill ask her.

my question how do you validate a member of an enum.
eg:
enum titletype = {Ms, Mrs, Mr, Dr}

if the user entered Ms how to i search the enum to see if it exists, just like if they entered Miss it would be an error.

thanks in advance
this is not something that you would use enums for.

I would do so like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
using std::string;

string prefixes[4] = {"Ms", "Mrs", "Mr", "Dr"};

bool is_valid_prefix(string p)
{
  for (int i=0; i < 4; i++)
  {
    if (p==prefixes[i])
      return true;
  }
  return false;
}
Last edited on
An enum would be used in a situation like:
1
2
3
4
5
6
7
enum STANCE
{
    STANDING,
    MOVING_RIGHT,
    MOVING_LEFT,
    JUMPING
};


Just as an example.

Go here and have a look at them as well - http://www.cprogramming.com/tutorial/enum.html
see thats the thing i dont want to use an enum but its a small section of this task i have been given and atm ive just been putting it off by using a string.

@Mythios: thanks for the linked it did help, now i just have to keep on tweaking to get it right

thanks for all your help
No problem ;)
see thats the thing i dont want to use an enum but its a small section of this task i have been given and atm ive just been putting it off by using a string.


That doesn't make a lot of sense to me since I don't know what the whole task is. I never did understand the original question. It doesn't make sense.
Topic archived. No new replies allowed.