Thread: enum

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    enum

    Hi I am new learner of C++.
    I am stuck with a program using enum.
    The program is to be written using enum which will display name of the student when we enter the roll no.
    Now I am not getting how to access the enum members using the integer input.
    Can you give some idea?

  2. #2

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by scwizzo View Post
    Printing Enums
    You might wonder what happens when you print out an enum: by default, you'll get the integer value of the enum. If you want to do something fancier than that, you'll have to handle it specially.


    So if I want to print out the enum,I must handle it specially.
    So what way?

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Depends on what you want to do.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    Ummm its very dependent on what you want to do... for example.. if you want to display certain text depending on the value of an enum youwould need to handle it in a switch case...
    so you would put in the student names where the enum is...

    Code:
    enum studentNumbers{ johnDoe = 0, janeDoe, brittanyHolliday... etc.
    }
    then you would handle it in something like this...
    Code:
    switch(valueToCompare){
    case 0:
    cout<< "John Doe's student number is 0"; 
    }
    I'm not sure if using flat out enum would be the easiest way to do this... I think i would make a student struct or something to go about dislplaying the information because of ease.. I'm guessing that you would want to display the students name with the number.. I'll make a little working program, but i got to know the intention, are you wanting to use a program to process the sudent number and print the student info or what? I'm not sure if using an enum is the best way to go about storing a student number, being you would have to go back to the program nd enter a new name for each new student.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What is it with people wanting enumerations to print out strings? That is not what they are for.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by jamort View Post
    Ummm its very dependent on what you want to do... for example.. if you want to display certain text depending on the value of an enum youwould need to handle it in a switch case...
    so you would put in the student names where the enum is...

    Code:
    enum studentNumbers{ johnDoe = 0, janeDoe, brittanyHolliday... etc.
    }
    then you would handle it in something like this...
    Code:
    switch(valueToCompare){
    case 0:
    cout<< "John Doe's student number is 0"; 
    }
    I'm not sure if using flat out enum would be the easiest way to do this... I think i would make a student struct or something to go about dislplaying the information because of ease.. I'm guessing that you would want to display the students name with the number.. I'll make a little working program, but i got to know the intention, are you wanting to use a program to process the sudent number and print the student info or what? I'm not sure if using an enum is the best way to go about storing a student number, being you would have to go back to the program nd enter a new name for each new student.
    No, this question is just only a question given by our teacher to implement the use of enum.So I need to use enum only and display the name but its seems there is some complex way out to do it.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by jamort View Post
    Ummm its very dependent on what you want to do... for example.. if you want to display certain text depending on the value of an enum youwould need to handle it in a switch case...
    so you would put in the student names where the enum is...

    Code:
    enum studentNumbers{ johnDoe = 0, janeDoe, brittanyHolliday... etc.
    }
    then you would handle it in something like this...
    Code:
    switch(valueToCompare){
    case 0:
    cout<< "John Doe's student number is 0"; 
    }
    I'm not sure if using flat out enum would be the easiest way to do this... I think i would make a student struct or something to go about dislplaying the information because of ease.. I'm guessing that you would want to display the students name with the number.. I'll make a little working program, but i got to know the intention, are you wanting to use a program to process the sudent number and print the student info or what? I'm not sure if using an enum is the best way to go about storing a student number, being you would have to go back to the program nd enter a new name for each new student.
    No, this question is just only a question given by our teacher to implement the use of enum.So I need to use enum only and display the name but its seems there is some complex way out to do it.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, there are a fixed (at compile time) number of students on some nominal roll. The user enters a number corresponding to an enumerator, and you display the corresponding student's name. Is that it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    106
    thats simple... all you have to do is enum all of the names.. then do a switch case with the numbers for printing so ill implement four names....

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    enum studentNumber{
        JohnDoe = 0, JohnSmith, JaneDoe, JoshShort
    };
    
    int main(){
        int student = 0;
        cout<< "Enter student number: ";
        cin>> student;
    
    
        switch(student)
        {
            case JohnDoe:
            //inset what you want to diplay here
            break;
    
            case JohnSmith:
            //inset what you want to diplay here
            break;
    
            case JaneDoe:
            //inset what you want to diplay here
            break;
    
            case JoshShort:
            //inset what you want to diplay here
            break;
    
            default:
            cout<< "Error Something went wrong!";
            break;
        }
    }
    this isnt really the best way to go about making a syste of this type, and it really could be somewhat misleading if one of the students tries to make a program like this allowing you to create new students
    Last edited by jamort; 08-20-2010 at 11:29 PM.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by laserlight View Post
    So, there are a fixed (at compile time) number of students on some nominal roll. The user enters a number corresponding to an enumerator, and you display the corresponding student's name. Is that it?
    Yes exactly.
    Can't we escape from using switch because using switch makes the enum worthless,since we could do that without using enum.

    Isn't there any scope for typecasting or like that, and the main problem is that the enum prints out the enum value not the enum name.Any idea?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rakeshkool27
    Can't we escape from using switch because using switch makes the enum worthless,since we could do that without using enum.
    You have a valid point. Unfortunately, enum is rather worthless is this case as Bubba implied in post #6.

    Quote Originally Posted by rakeshkool27
    Isn't there any scope for typecasting or like that, and the main problem is that the enum prints out the enum value not the enum name.Any idea?
    Essentially, you are trying to map a number to a name. Something like an array or a std::map, not an enum, is more appropriate and effective. Since you must use an enum, just bite the bullet and follow the silliness to its conclusion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    106
    yeah.. um im a little confused as why they would instruct to use enums for something like this... they could have came up with a lot better challenge, and i used switch to handle the enum because it is something that i expected you would know how to use

  14. #14
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by laserlight View Post
    Essentially, you are trying to map a number to a name. Something like an array or a std::map, not an enum, is more appropriate and effective. Since you must use an enum, just bite the bullet and follow the silliness to its conclusion.
    So there seems printing out the name using enum is not going to work.It seems there is no way out for this.
    So what I must do is to tell the teacher that its not possible.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    106
    No.. I would give him some code implementing enums.. even if its not exactly "correct"... in the end you know you will get a grade.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 10:29 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM