How to make this use a enum

This is a discussion on How to make this use a enum within the C++ Programming forums, part of the General Programming Boards category; I'm currently reading Beginning C++ Through Game Development, 2nd Edition and there's an exercise that reads like this: "Rewrite the ...

  1. #1
    Registered User Trennto's Avatar
    Join Date
    Jul 2008
    Location
    Orlando, FL
    Posts
    29

    How to make this use a enum

    I'm currently reading Beginning C++ Through Game Development, 2nd Edition and there's an exercise that reads like this: "Rewrite the Menu Chooser program from this chapter using an enumeration to represent difficulty levels. The variable choice will still be of type int."

    I need some help solving this exercise. Here's the code for Menu Chooser:

    Code:
    // Menu Chooser
    // Demonstrates the switch statement
    
    #include <iostream>
    using namespace std;
    
    int main() 
    {
    	cout << "Difficulty Levels\n\n";
    	cout << "1 - Easy\n";
    	cout << "2 - Normal\n";
    	cout << "3 - Hard\n\n";
    
    	int choice;
    	cout << "Choice: ";
    	cin >> choice;
    
    	switch (choice)
    	{
    	case 1:	
    			cout << "You picked Easy.\n";
    			break;
    	case 2:	
    			cout << "You picked Normal.\n";
    			break;
    	case 3:	
    			cout << "You picked Hard.\n";
    			break;
    	default:
    			cout << "You made an illegal choice.\n";
    	}
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    enum GAMEMODE
    {
       EASY,
       NORMAL,
       HARD
    };
    
    switch(choice)
    {
     case EASY:
       //bla bla
     case NORMAL:
       //bla bla
      case HARD:
       //bla bla
    }
    that is how it works. you need to figure out where to put it all

  3. #3
    Registered User Trennto's Avatar
    Join Date
    Jul 2008
    Location
    Orlando, FL
    Posts
    29
    It works, thank you. Here's the final (just in case you want to see it):

    Code:
    // Menu Chooser
    // Demonstrates the switch statement
    
    #include <iostream>
    using namespace std;
     
    int main() 
    {
        enum GAMEMODE {EASY,MEDIUM,HARD};
        
        cout << "Difficulty Levels\n\n";
        cout << "1 - Easy\n";
        cout << "2 - Normal\n";
        cout << "3 - Hard\n\n";
    
        int choice;
        cout << "Choice: ";
        cin >> choice;
    	
        choice -= 1;
        
        switch (choice)
        {
             case EASY:	
                  cout << "You picked Easy.\n";
                  break;
             case MEDIUM:	
                  cout << "You picked Normal.\n";
                  break;
             case HARD:	
                  cout << "You picked Hard.\n";
                  break;
             default:
                  cout << "You made an illegal choice.\n";
        }
    
        return 0;
    }
    by the way, why do I need to decrease the value of choice in order to get the right answer?
    Last edited by Trennto; 07-02-2008 at 07:02 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    It's far from useless. It simplifies code greatly.
    Also, consider creating a variable OF the enum type. This will ensure it can only hold the values declared inside the enum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Trennto's Avatar
    Join Date
    Jul 2008
    Location
    Orlando, FL
    Posts
    29
    I want to be able to understand enums better, so I have some questions:

    • How does the switch use the enum if they werent connected in any way?
    • Why do I need to decrease the value of choice in order to get the correct value?
    • How would I use a enum variable in this situation?
    • What are the exact benefits of an enum as opposed to plain constants?


    Sorry about all the questions, you dont have to answer all of them at once.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> How does the switch use the enum if they werent connected in any way?
    An enum is just an integral value, which is all the switch needs. Each enum item represents some integer, so it works the same as your original code.

    >> Why do I need to decrease the value of choice in order to get the correct value?
    The default values for enums start with 0 and increment by 1, so EASY has a value of 0. You could change this by using:
    Code:
    enum GAMEMODE {EASY=1,MEDIUM,HARD};
    >> How would I use a enum variable in this situation?
    Use GAMEMODE as the type instead of int. You might have problems reading into a GAMEMODE with cin, but you can use it in other ways.

    >> What are the exact benefits of an enum as opposed to plain constants?
    The enum automatically orders the constants for you, making maintenance easier. It also creates a separate type that can enforce some extra rules.

  7. #7
    Registered User Trennto's Avatar
    Join Date
    Jul 2008
    Location
    Orlando, FL
    Posts
    29
    Thank you very much, you answered my questions very well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 10:16 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 05:19 AM
  3. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 12:50 PM
  5. Replies: 6
    Last Post: 04-20-2002, 07:35 PM

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