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!
Re: I cannot input enum class value
Where do you invoke stream extraction for a type Genre?
Re: I cannot input enum class value
Quote:
Originally Posted by
2kaud
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;
}
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 :D