Thread: enumeration with switch case?

  1. #1
    Shadow12345
    Guest

    enumeration with switch case?

    Here is the planet class and constructor which randomly determines the planets status:
    Code:
    class Planet {
    public:
    	Planet();
    	enum { Ally, Governemnt, Neutral } Status;	//USED TO DETERMINE THE PLANETS STATUS IN THE GAME
    private:
    };
    
    Planet::Planet() {
    	int RandomStatus = rand()%3;	//RANDOMLY SETS THE STATUS OF THE PLANET
    	switch(RandomStatus) {
    	case 0:
    		Status = Ally;
    		break;
    	case 1:
    		Status = Governemnt;
    		break;
    	case 2:
    		Status = Neutral;
    		break;
    	default:
    		Status = Neutral;
    	}
    }
    Here i have an instance of the planet class
    Code:
    Planet *Planet1 = new Planet;
    and lastly I have a thing that tests the planet's status to make sure it works:
    Code:
    	char *AllyStatus = "The Planet is sided with the player";
    	char *GovtStatus = "The planet is sided with the goverment";
    	char *Neutral = "The planet is neutral";
    
     while(!done)         // Loop That Runs While done=FALSE
     {
    	 switch(Planet1->Status) {
    	case Ally:
    		TextOut(hDc, 0, 0, AllyStatus, sizeof(AllyStatus));
    		break;
    	case Government:
    		TextOut(hDc, 0, 0, GovernmentStatus, sizeof(GovernmentStatus));
    		break;
    	case Neutral:
    		TextOut(hDc, 0, 0, Neutral, sizeof(Neutral));
    		break;
    	default:
    		break;
    	 }
    ...
    ...
    ...
    For some reason I get these errors:
    --------------------Configuration: lesson1 - Win32 Debug--------------------
    Compiling...
    Lesson1.cpp
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(738) : error C2065: 'Ally' : undeclared identifier
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(738) : error C2051: case expression not constant
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(741) : error C2065: 'Government' : undeclared identifier
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(741) : error C2051: case expression not constant
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(742) : error C2065: 'GovernmentStatus' : undeclared identifier
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(744) : error C2051: case expression not constant
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(749) : warning C4065: switch statement contains 'default' but no 'case' labels
    Error executing cl.exe.

    lesson1.exe - 6 error(s), 1 warning(s)


    That's a lot of errors! Ouch!
    Last edited by Shadow12345; 09-26-2002 at 08:27 AM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    first, your switch that sets status is kinda funny. because:

    Player, Governemnt, Neutral are equal to 0, 1, 2
    Code:
    Planet::Planet() {
    	Status = rand()%3;	//RANDOMLY SETS THE STATUS OF THE PLANET
                    
    }
    that would be enough

    I haven't looked at the rest yet
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    your errors if I'm not mistaken are because the "type" of Status is not available outside the class. I think you should typedef a "type" for Status and they you will have access to the names Ally, etc...
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Shadow12345
    Guest
    uhh... no
    Code:
    Status = rand()%3;
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(107) : error C2440: '=' : cannot convert from 'int' to 'enum '

    I thought enums were ints by default!

    nuts

  5. #5
    Shadow12345
    Guest
    The status IS available outside of the class because it is a public member, that means it can be accessed in any other function!
    Well that's what i thought neways

  6. #6
    Shadow12345
    Guest
    For the typedef
    Do you mean something like:
    Code:
    typedef enum Status { Ally, Governemnt, Neutral };
    Now I have
    Code:
    Status PlanetStatus;
    as a public member and

    Code:
    PlanetStatus = rand()%3;
    but I still get errors

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't have all of your code so I improvised
    Code:
    typedef enum {Ally, Government, Neutral} status;
    
    class Planet {
    public:
    	Planet();
    	status Status;	//USED TO DETERMINE THE PLANETS STATUS IN THE GAME
    private:
    };
    
    Planet::Planet() {
    	int RandomStatus = rand()%3;	//RANDOMLY SETS THE STATUS OF THE PLANET
    	switch(RandomStatus) {
    	case 0:
    		Status = Ally;
    		break;
    	case 1:
    		Status = Government;
    		break;
    	case 2:
    		Status = Neutral;
    		break;
    	default:
    		Status = Neutral;
    	}
    }
    
    
    int main(int argc, char* argv[])
    {
    	Planet *Planet1 = new Planet;
         char *AllyStatus = "The Planet is sided with the player";
    	char *GovtStatus = "The planet is sided with the goverment";
    	char *Neutralstr = "The planet is neutral";
    
         switch(Planet1->Status) {
    	     case Ally:
                   printf(AllyStatus);
    		     break;
    	     case Government:
                   printf(GovtStatus);
    		     break;
    	     case Neutral:
                   printf(Neutralstr);
    		     break;
    	     default:
    		     break;
    	      }
    
    	return 0;
    }
    and this does compile.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Available they might be but you will need to prefix with Planet:: so the compiler can have a clue what your enum is.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Stoned_Coder
    Available they might be
    yoda? is that you?

    sorry, it just struck me as humorous
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    taken earlier...
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    LOL!!!!!!!!

  12. #12
    Shadow12345
    Guest
    Okay that does work. I thought you wouldn't be able to declare types of 'status' because it came after the enum definition...I thought doing it that way would make it so you can only have that particular enumerated type and no more.

    I have only one error now:
    C:\Documents and Settings\04thibaultc\Desktop\9 - 26 - 02 doens't compile\Lesson1.cpp(744) : error C2051: case expression not constant

    It points to the Neutral of my switch() case, that is really weird, why is it doing that?

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's because you reused a name. Neutral is one of your enum values and also the name of a string. I changed that in my edited version to Neutralstr
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Shadow12345
    Guest

    Thumbs up

    Ohhhhhh

    Now I have ANOTHER problem, the TextOut function only actually outputs the first word of the char and it blinks like it's on crack or something
    Last edited by Shadow12345; 09-26-2002 at 09:58 AM.

  15. #15
    Shadow12345
    Guest
    Here is a picture of the top left corner where the text is supposed to be displayed:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM