user2699298
user2699298

Reputation: 1476

Displaying, updating and validating values of enum in C++

#include <iostream>

enum SEX {MALE, FEMALE};

int main(int argc, char**argv)
{
    enum SEX the_sex = MALE;
    return 0;
}

How can I display the_sex value on the terminal or console, accept values from the terminal or console to update the value of the_sex and how can I valid the input for the_sex variable?

Upvotes: 4

Views: 743

Answers (3)

Shoe
Shoe

Reputation: 76240

How can I accept values from the terminal or console to update the value of the_sex and how can I valid the input for the_sex variable?

The input can be represented by whatever you want: an integer (1 for male, 2 for female), a char ('M' for male, 'F' for female), a std::string. Here's a code example for the char version:

char in;
std::cin >> in;
switch (in) {
case 'M':
    the_sex = MALE;
    break;
case 'F':
    the_sex = FEMALE;
    break;
default:
    // invalid input
    break;
}

or, here's the std::string version:

std::string in;
std::cin >> in;
        if (in == "MALE") the_sex = MALE;
else    if (in == "FEMALE") the_sex = FEMALE;
else    // invalid input

How can I display the_sex value on the terminal or console?

You can simply use a switch statement to print out the value of your SEX variable:

std::ostream& operator<<(std::ostream& os, SEX the_sex) { 
    switch (the_sex) {
    case MALE:
        os << "MALE";
        break;
    case FEMALE:
        os << "FEMALE";
        break;
    }
    return os;
}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

To output the value of the enumeration you can write simply

std::cout << the_sex;

The enumerator will be displayed as an integer value (in this case as 1).

To get and validate a value for the enumeration you can use for example the following loop

int e;

do
{
   std::cout << "Enter the sex of the person (0 - FEMALE, 1 - MALE): ";
} while ( std::cin >> e && e != 0 && e != 1 );

if ( std::cin ) the_sex = static_cast<SEX>( e );

Upvotes: 2

user1810087
user1810087

Reputation: 5334

i'm using a macro for doing this.

#define name_num(NAME, ...)                                                    \
class NAME {                                                                   \
                                                                               \
public:                                                                        \
                                                                               \
  enum   enums{NAME_NUM_BEGIN_OF_ENUM_MAP,                                     \
               __VA_ARGS__,                                                    \
               NAME_NUM_END_OF_ENUM_MAP};                                      \
                                                                               \
  using  map_type = boost::bimap<enums, std::string>;                          \
                                                                               \
  NAME(std::string const& str) {                                               \
    std::vector<std::string> v;                                                \
    boost::split(v, str, boost::is_any_of(", "), boost::token_compress_on);    \
    map_type m;                                                                \
                                                                               \
    for(int i=NAME_NUM_BEGIN_OF_ENUM_MAP+1; i!=NAME_NUM_END_OF_ENUM_MAP; i++)  \
      map_.insert(map_type::value_type(static_cast<enums>(i), v[i-1]));        \
  }                                                                            \
                                                                               \
  std::string string(enums val)        { return map_.left.at(val); }           \
                                                                               \
  enums number(std::string const& val) { return map_.right.at(val); }          \
                                                                               \
private:                                                                       \
  map_type map_;                                                               \
} NAME(#__VA_ARGS__)

it creates a usual enum list which can be used as usual (e.g. in switches). also it uses boost bimap to map the enums with the coresponding strings.

the first parameter of the macro is the name of the class and instance which is used to access the enums and the methods.

finding the enum you use number and finding the string you use string method. if string (in method number) isn't pointing to a valid enum a std::out_of_range("bimap<>: invalid key") will be thrown.

see this example.

Upvotes: 2

Related Questions