Enum w/in Struct Help!

This is a discussion on Enum w/in Struct Help! within the C++ Programming forums, part of the General Programming Boards category; I'm getting two errors. I think I'm declaring my Enum wrong please help. the code is attached:...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Enum w/in Struct Help!

    I'm getting two errors. I think I'm declaring my Enum wrong please help.
    the code is attached:
    Attached Files Attached Files

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Enums are not exactly complete types: they are a group of constant numbers given a label, and while they are actually surprisingly flexible, a user cannot interact directly with an enum, as you tried. I would just make the students' gender and class a string.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    okay how can I modify program so that I can still use the students gender and class as an enum? Would I not cin>>person.classification or person.sex and use something like a if else?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    It's best if you just copy-paste your code and name the errors you get. It's not a long piece of code and some of us may not want to download, open and compile your code just to see what is wrong. Please read the announcement at the top of this forum.

    Anyway, you need to overload the input operator for each of your enumerations.

    Code:
    istream& operator>>(istream &is, SexType &obj) {
        int input;
    
        if (is >> input) {
            switch (input) {
                case 0: case 1:
                    obj = SexType(input);
                    break;
          }
       }
    
      return is;
    }
    And you need to do the same to the other enum.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    In another approach, you would have to read in their class and gender in a temporary string. I would think then a simple if/else can then determine what you should assign to those fields. For example, with gender:

    Code:
    string temp;
    cout<<"Enter the gender: ";
    cin>>temp;
    
    if (temp == "male")
        student.sex = Male;
    else if(temp == "female")
        student.sex = Female;
    else 
        student.sex = Undisclosed;
    Something like that. In non-trivial programs, you'd want to make sure that the string was in lowercase so that the comparisons work like the example. Mario's approach doesn't work the best though, as this is particularly error-prone.
    Last edited by whiteflags; 07-04-2006 at 02:54 PM.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Thank You!

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Quote Originally Posted by citizen
    Mario's approach doesn't work the best though
    Actually, there's a fundamental flaw on my code.

    Code:
    /* ... */
    switch (input) {
        case 0: case 1:
            obj = Sex(input);
            break;
        default:
            is.setstate(istream::badbit);
    /* ... */
    Now, at least it has some form of error-checking.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    But you're still using an int. You can't get away from the basic if/else tree really. Your user will not type a number for your switch statement to work, rather s/he would type something like "male" or "female" and I use another else for undisclosed: which basically accepts random input but it works.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Oh. I was aware of that. Was just mentioning that adding to a poor solution I was also not doing a proper error checking on the stream
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. syntax error: enum and typedef struct
    By sagitt13 in forum C Programming
    Replies: 2
    Last Post: 10-31-2006, 03:17 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21