-
July 27th, 2015, 01:05 PM
#1
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!
-
July 27th, 2015, 03:01 PM
#2
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 28th, 2015, 02:55 AM
#3
Re: I cannot input enum class value
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;
}
-
July 28th, 2015, 03:18 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|