Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 293673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:17:50+00:00 2026-05-12T06:17:50+00:00

I was wondering if enumeration is commonly used with user input. I’m doing an

  • 0

I was wondering if enumeration is commonly used with user input. I’m doing an exercise in which in my Book class I have to create an enum Genre with different genre enumerators such as fiction, non, fiction etc.

When the user uses the program, he/she is asked for certain information about the book being stored. For a genre, normally I would just do this with a string function and restrict it to certain names with if statements.

However, I’m not sure how to accomplish the same process with an enumerated type, nor do I know if it’s even supposed to be used for that sort of thing. Here is the code if you’re interested.

#include "std_lib_facilities.h"

//Classes-----------------------------------------------------------------------

class Book{
public:
       Book(){}; // default constructor
       //operators
       friend ostream& operator<<(ostream& out, const Book& val);
       bool Book::operator==(const Book& check)
       //enumerators
       enum Genre{
            fiction, nonfiction, periodical, biography, children};
       //member functions
       string title();
       string author();
       int copyright();
       void ISBN();
       bool checkout();
private:
        string title_;
        string author_;
        int copyright_;
        int ISBN1;
        int ISBN2;
        int ISBN3;
        char ISBN4;
        bool checkout_;
};

// Error Function---------------------------------------------------------------

void _error(const string& s)
{
     cout << endl;
     cout << "Error: " << s << endl;
     cout << endl;
}

// Member Functions-------------------------------------------------------------

string Book::title()
{
       cout << "Title: ";
       getline(cin,title_);
       cout << endl;
       return title_;
}

string Book::author()
{
       cout << "Author: ";
       getline(cin,author_);
       cout << endl;
       return author_;
}

int Book::copyright()
{
    cout << "Copyright: ";
    cin >> copyright_;
    cout << endl;
    return copyright_;
}

void Book::ISBN()
{
     cout << "ISBN (Use spaces): ";
     cin >> ISBN1 >> ISBN2 >> ISBN3 >> ISBN4;
     if((ISBN1<0) || (ISBN2<0) || (ISBN3<0) || (ISBN1>9) || (ISBN2>9) || (ISBN3)>9)
               _error("Must be single digit.");
     else if(!isdigit(ISBN4) && !isalpha(ISBN4))
               _error("Must be single digit or letter.");
     else{ cout << endl;
           return;}
}

bool Book::checkout()
{
     char check;
     cout << "Checked out?(Y or N): ";
     cin >> check;
     switch(check){
     case 'Y':
          cout << endl;
          return true;
          break;
     case 'N':
          cout << endl;
          return false;
          break;
     default: 
              _error("Must be Y or N.");}
}

// Operator Overloads-----------------------------------------------------------

ostream& operator<<(ostream& out, const Book& val){
         out << "Title: " << val.title_ << endl;
         out << "Author: " << val.author_ << endl;
         out << "ISBN: " << val.ISBN1 << "-" << val.ISBN2 << "-" << val.ISBN3 << "-" << val.ISBN4 << endl;
         out << endl;
         return out;}

bool Book::operator==(const Book& check){
     return((ISBN1 == check.ISBN1) && (ISBN2 == check.ISBN2) && (ISBN3 == check.ISBN3)
             && (ISBN4 == check.ISBN4));}

// Main-------------------------------------------------------------------------   

int main()
{
    bool finished = false;
    char notfinished;
    while(!finished)
    {
      Book book;
      book.title();
      book.author();
      book.copyright();
      book.ISBN();
      book.checkout();
      cout << "Do you wish to store another book?(Y or N): ";
      cin >> notfinished;
      if(notfinished == 'Y'){ 
                     cin.ignore();
                     cout << endl;}
      else if(notfinished == 'N') finished = true;
      else _error("Must be Y or N");
      }
     keep_window_open();
     }

Note that some things aren’t being used at the moment because the feature they are a part of hasn’t been fully implemented yet (storing in a library, outputting books, etc.)

So what would it take to accept user input for the enumerators listed, if even possible? I was thinking something along the lines of making a Genre variable. Then having a function where the user inputs for cin>>variable. However, I’m guessing that the function wouldn’t understand an input like ‘fiction’ and would only accept the enumerator values and input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:17:50+00:00Added an answer on May 12, 2026 at 6:17 am

    C-style enums are not terribly useful for this purpose, since there’s no way to recover the original string name. You could make some switch-based mechanism, but at that point you may as well just set up your own way of doing it all that works with your user I/O requirements without shoehorning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a class called Singularity and within it, a method to create a
I have the following enumeration: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION =
If have an enumeration like so: typedef enum { ValueOne = /* some arbitrary
Wondering what the best / good way of doing this would be in jQuery.
Wondering if someone could help me. I have next to no knowledge with Ajax,
I was wondering if anyone could tell me how to represent the enumeration of
I was wondering if the following will have an effect as I intend it.
For my book class I'm storing an ISBN number and I need to validate
Wondering if this is possible or something with this effect. public class MyModel {
I am wondering about the following fact. I have a data repository that returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.