Thread: variable of type enum to be entered by user

  1. #1
    Registered User Clayg's Avatar
    Join Date
    Nov 2011
    Location
    Hawaii
    Posts
    20

    variable of type enum to be entered by user

    This is an assignment for an online course:

    "Create a set/list of enumerated constants called week that contain the days of the week. Have a variable named today of type week.Assign a value to today. if it is Mon through Fri printf("Blah")
    if it is Sat or Sun... BLAh."

    In RL if I were to use this I would want the numbers to be on this side of the program and have the user enter a string to display the correct output. I tried to do it several different ways but was only succesful when entering the numerical version of the enum on the user side. If I entered the word "Mon", on my switch it would default or run time fail. If I entered 1 it would display the correct output. So why use this enumerated number set up?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    enumerations are named constants visible only to the programmer.

    If you want the user to be able to enter 1 or "monday", then you must code for this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Clayg's Avatar
    Join Date
    Nov 2011
    Location
    Hawaii
    Posts
    20
    I tried to set it up with the user entering a day of the week, but I couldn't get a %s to beaccepted, only %d. So are enumerations only for the programer to remember what the ints are related too?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Clayg View Post
    I tried to set it up with the user entering a day of the week, but I couldn't get a %s to beaccepted, only %d. So are enumerations only for the programer to remember what the ints are related too?
    Yes... but you could define a lookup table for it.
    ....like...making an array of strings where the enum value of a day corresponds to the data stored at that particular index.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Clayg View Post
    I tried to set it up with the user entering a day of the week, but I couldn't get a %s to beaccepted, only %d. So are enumerations only for the programer to remember what the ints are related too?
    Yes... It's not user accessible.

    As manasij is suggesting you can use an array of user entered strings. This will let you search for a string and get the index or go to the index and get the string... which is most likely what you're trying to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to declare an external variable of type "enum"?
    By Adam Rinkleff in forum C Programming
    Replies: 28
    Last Post: 06-23-2011, 09:52 PM
  2. turns the name entered by the user
    By Magic-Man-fci in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2011, 12:14 AM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. How to hide what a user has entered with *'s
    By stuartg123 in forum C++ Programming
    Replies: 14
    Last Post: 04-16-2007, 01:29 PM
  5. use user entered formula to calculate something
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 12-07-2001, 02:21 AM