c++ enum and using them to input from user

前端 未结 3 1996
时光说笑
时光说笑 2021-02-08 05:34

I have an enum in my code that is the following: enum Status {In-Active, Active};. A status object is passed to a Person object as a parameter, so I wa

相关标签:
3条回答
  • 2021-02-08 05:51

    You can't read enum values directly, you'll need a std::map that maps user input to an enum value.

    std::map<std::string,Status> m;
    m["In-Active"] = In-Active;
    m["Active"] = Active;
    
    std::string sstatus;
    cin >> sstatus;
    Status status = m[sstatus];
    
    0 讨论(0)
  • 2021-02-08 05:54
    • Enum objects are being parsed at compile time and it can only contain constant integer values.

    • Person p(name, age, status); //Here without using status object you should send a value directly like "In-Active" or "Active". So there would be no input from the user regarding th status field.

    enum Status {InActive, Active};

    • Here Status is a enum class and any object created would contain values "Inactive" or "Active". If you try to assign some other value you would get an compiler error.

    Status status;

    • Here status is an enum object created and it contains garbage value.

    cout << "Enter status: "; cin >> status;

    • Here you are asking user for an input for an enum object which is totally invalid for the compiler.
    0 讨论(0)
  • 2021-02-08 06:06

    You can also wrap the enum inside a class and provide operator>>. This is a modified example from this link http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum

    #include <iostream>
    
    template<typename def>
    class safe_enum : public def
    {
    public:
      int val;
      safe_enum(int v) : val(v) {}
    //  inner underlying() const { return val; }
      bool operator == (const safe_enum & s) const { return this->val == s.val; }
      bool operator != (const safe_enum & s) const { return this->val != s.val; }
      bool operator <  (const safe_enum & s) const { return this->val <  s.val; }
      bool operator <= (const safe_enum & s) const { return this->val <= s.val; }
      bool operator >  (const safe_enum & s) const { return this->val >  s.val; }
      bool operator >= (const safe_enum & s) const { return this->val >= s.val; }
    };
    
    struct status_def {
      enum type { NoValue, InActive, Active };
    };
    typedef safe_enum<status_def> Status;
    
    std::istream& operator>>(std::istream& Istr, Status& other)
    {
        std::cout << "Enter Status value: " << std::endl; 
        //here the logic to handle input error
        Istr >> other.val;
        return Istr;
    }
    
    std::ostream& operator<<(std::ostream& os,const Status& toPrint)
    {
        if (toPrint.val == Status::Active)
            return os << "Active";
        if (toPrint.val == Status::NoValue)
            return os << "NoValue";
        if (toPrint.val == Status::InActive)
            return os << "InActive";
        throw;
    }
    
    int main(void)
    {
        Status test(Status::NoValue);
        std::cout << test << std::endl;
        std::cin >> test;
        std::cout << test << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题