• Advertisement
Sign in to follow this  

enum problem

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have an enumeration problem in C++ could someone tell me whats up with this code? the compiler tells me several things, that theres a "parse error" before the 'numeric constant' in the enum dec on line 5 also, when i declare ERRCODE result, it says its storage size is unknown. and lots more...but i think it all leads back to the first problem #include <iostream> using namespace std; enum ERRCODE { SUCCESS, ERROR }; ERRCODE Factor(int , int&, int&); int main() { int number, squared, cubed; ERRCODE result; cout<<"enter a number (0-20): "; cin>>number; result=Factor(number, squared, cubed); if(result==SUCCESS) { cout<<"\nnumber: "<<number<<endl; cout<<"\nsquared: "<<squared<<endl; cout<<"\ncubed: "<<cubed<<endl; } else cout<<"ERROR FOUND!!"<<endl; char res; cin>>res; return 0; } ERRCODE Factor(int n, int &rSquared, int &rCubed) { if(n>20) return ERROR; else { rSquared=n*n; rCubed=n*n*n; return SUCCESS; } }

Share this post


Link to post
Share on other sites
Advertisement
glancing once at the code....you need to start your enum...you cant just declare an enum without at least initializing the first one...at least im pretty sure...


enum ERRCODE { SUCCESS = 0, ERROR };

try that...if it still breaks...its most likely something else

Share this post


Link to post
Share on other sites
Quote:
Original post by Tang of the Mountain
enum ERRCODE { SUCCESS = 0, ERROR };


You only need to set the first one if you want it to start at somthing besides 0.

Share this post


Link to post
Share on other sites
i initialized it to zero(SUCCESS in the enum) but it still did not work
and i use Dev 4.9.8.0, for the record
oh, and sorry about the long code, i just found out about the


tags; i'll use them from now on
i appreciate any help given

Share this post


Link to post
Share on other sites
If it works on MSVC and not Dev-cpp, the only guess I have is that "SUCCESS" or "ERROR" is already defined as somthing. This is just a guess, but try changig the names to SUCCESS1 and ERROR1 or somthing...

Share this post


Link to post
Share on other sites
wow, that actually worked
but where in iostream would ERROR and SUCCESS be defined?
i can see ERROR being in there, but not the other...

Share this post


Link to post
Share on other sites
Err, there is another forum for programming spcefic questions, but whatever.

Share this post


Link to post
Share on other sites
as a general rule of thumb, try to avoid really generic names. I know they're convenient, but it also means they have been convenient to thousands of other developers out there. I'm probly shooting myself saying this, but try to make the constant names a *bit* cryptic (yet readable) like appending an abbreviation or something. It makes names clashes all the less likely.

also, utilize namespaces when using enumerators and C++ style constants. #define constants tend to cause name pollutions and i dun think they can be contained within namespaces, but correct me if i'm wrong. Namespaced constants/enums are a better way to use 'common names' like success or error as long as they do not collide with preprocessor constants.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement