I'm getting two errors. I think I'm declaring my Enum wrong please help.
the code is attached:
I'm getting two errors. I think I'm declaring my Enum wrong please help.
the code is attached:
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.
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?
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.
And you need to do the same to the other enum.Code:istream& operator>>(istream &is, SexType &obj) { int input; if (is >> input) { switch (input) { case 0: case 1: obj = SexType(input); break; } } return is; }
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.
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:
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.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;
Last edited by whiteflags; 07-04-2006 at 02:54 PM.
Thank You!
Actually, there's a fundamental flaw on my code.Originally Posted by citizen
Now, at least it has some form of error-checking.Code:/* ... */ switch (input) { case 0: case 1: obj = Sex(input); break; default: is.setstate(istream::badbit); /* ... */
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.
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.
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
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.