CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2015
    Posts
    10

    Question I cannot input enum class value

    I have defined a enum class:

    Code:
    enum class Genre
    {
    	fiction, nonfiction, periodical, biography, children
    };
    And another class:

    Code:
    class Book
    {
    public:
    	string f_title() { return title; }
    	string f_author() { return author; }
    	string f_isbn() { return isbn; }
    	Genre f_genre() { return g; }
    	int f_day();
    	int f_month();
    	int f_year();
    	Book(string tit, string aut, string isbn, Genre g);
    	Book(string tit, string aut, string isbn);
    	Book();
    	class Invalid {};
    private:
    	string isbn, title, author;
    	int day, month, year;
    	Genre g;
    };
    I am stuck because I am not able to directly input values because I am not able to correctly set helper function. I had to create two different ones:

    Code:
    istream& operator>>(istream& is, Book& n)
    {
    	string title, author, isbn;
    
    	getline(is,title);
    	getline(is,author);
    	getline(is,isbn);
    	isbn.resize(7);
    
    	if (!is) return is;
    
    	n = Book{ title,author,isbn };
    
    	return is;
    }
    
    istream& operator>>(istream& is, Genre& g)
    {
    	int i;
    	is >> i;
    	g = (Genre)i;
    
    	return is;
    }
    but my program skips the Genre helper function and returns to me the default value "N/A".

    How can I fix it?

    Thanks!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: I cannot input enum class value

    Where do you invoke stream extraction for a type Genre?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  3. #3
    Join Date
    Jul 2015
    Posts
    10

    Re: I cannot input enum class value

    Quote Originally Posted by 2kaud View Post
    Where do you invoke stream extraction for a type Genre?
    Do you mean this?

    Code:
    ostream& operator<<(ostream& os, Book& n)
    {
    	return os << "Title: " << n.f_title() << endl
    		<< "Author: " << n.f_author() << endl
    		<< "ISBN: " << n.f_isbn()  << endl
    		<< "Genre: " << n.f_genre() << endl;
    }

  4. #4
    Join Date
    Jul 2015
    Posts
    10

    Re: I cannot input enum class value

    I solved the issue!

    I have split out the helper function in two distinct, then in main() I have separated cin creating a specific object for Genre enum class. It works perfectly! Thank you anyway

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured