simple problem: MenuChooserProgram using enumeration

This is a discussion on simple problem: MenuChooserProgram using enumeration within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have recently began learning C++ and have been reading and doing all the exercises in my beginners ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    8

    simple problem: MenuChooserProgram using enumeration

    Hi everyone, I have recently began learning C++ and have been reading and doing all the exercises in my beginners C++ book. I'm having some trouble with a particular problem and was hoping someone on this forum would be willing to offer some aid, whether it's an answer or a point in the right direction.

    Here is the original MenuChooserProgram source code:
    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;
    }

    Now the question is:
    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 know it's quite the simple problem but like I said I am new and could use a point in the right direction. I hope I can find the help I need here. Until then I'll continue to try different options.

    Thank you all,

    Tyler

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Code:
    //Chapter 2 - question 2
    //Menu Chooser Program using enumeration
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int choice;
    
    
    	enum difficulty {EASY = 1, NORMAL, HARD};
    	difficulty myDifficulty;
    	myDifficulty == choice;
    
    	cout << "Difficulty Levels: \n\n";
    	cout << "1 - Easy.\n";
    	cout << "2 - Normal.\n";
    	cout << "3 - Hard.\n";
    
    	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";
    	}
    
    	system ("pause");
    
    	return 0;
    }
    this seems to work. sorry if it's messy I just threw it together.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    sorry for triple post.
    I forgot to output the "Choice: " other than that I think I nailed it.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Enumeration is an integral data type (like int or long), which is used to represent consecutive constant numbers. Enumerations are defined this way:

    Code:
    enum MenuChoice {
        MC_EASY = 1,
        MC_NORMAL,
        MC_HARD,
    };
    This defines 3 constants of type MenuChoice: MC_EASY = 1, MC_NORMAL = 2, and MC_HARD = 3.
    You should know what to do with them now.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by kmdv View Post
    Enumeration is an integral data type (like int or long), which is used to represent consecutive constant numbers. Enumerations are defined this way:

    Code:
    enum MenuChoice {
        MC_EASY = 1,
        MC_NORMAL,
        MC_HARD,
    };
    This defines 3 constants of type MenuChoice: MC_EASY = 1, MC_NORMAL = 2, and MC_HARD = 3.
    You should know what to do with them now.
    I do appreciate the help.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Firstly, turn your compiler warning levels up and pay attention to the warnings. Remember that the code executes in exactly the order that it appears here. Be careful not to get your cart before the horse.

    Then you might want to consider actually using the enums! (*facepalm*)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    I did not check for the replies before posting.

    Usually data types are not defined within functions. You may find it better to declared it outside of main, in case you want to use it in a different part of your program later.

    The first problem is a comparison operator:

    Code:
    enum difficulty {EASY = 1, NORMAL, HARD};
    difficulty myDifficulty;
    myDifficulty == choice;
    Which does not assign any value to myDifficulty.

    Secondly, the choice variable has not any value assigned yet, you would assign some garbage (if you would do).

    Thirdly, you have not used your difficulty variable in any way. The switch case still checks 'choise' instead of 'difficulty'.

    There is even more to say.
    Last edited by kmdv; 07-30-2011 at 03:34 AM.
    I never put signature, but I decided to make an exception.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by iMalc View Post
    Firstly, turn your compiler warning levels up and pay attention to the warnings. Remember that the code executes in exactly the order that it appears here. Be careful not to get your cart before the horse.

    Then you might want to consider actually using the enums! (*facepalm*)
    the first code i put up was the original menuchooser program without enums. the objective was to make a new version with enums which i think i did in my second reply. I could be wrong I guess but It seems to work...

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by kmdv View Post
    I did not check for the replies before posting.

    So what is the problem now? Usually data types are not defined within functions. You may find it better to declared it outside of main, in case you want to use it in a different part of your program later.
    sorry, ignore what I said. I just saw your other post. I'll look over things thank you.
    Last edited by D1ffsta; 07-30-2011 at 03:36 AM.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by D1ffsta View Post
    the first code i put up was the original menuchooser program without enums. the objective was to make a new version with enums which i think i did in my second reply. I could be wrong I guess but It seems to work...
    It works no more or less than the first piece of code did. The thing is though, that you're not actually using the enums. You've declared them, but you're not using them, which kinda misses the point.
    They serve no more purpose than being documentary so far.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Oh sorry I understand now. hmmm... i guess back to the drawing board.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Code:
    //enums try again
    
    #include <iostream>
    using namespace std;
    
    enum difficulty {E_EASY = 1, E_NORMAL, E_HARD};
    
    int main()
    {
    	int choice;
    
    	cout << "Difficulty Level: \n\n";
    	cout << "1-Easy.\n";
    	cout << "2-Normal.\n";
    	cout << "3-Hard.\n";
    
    	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. Try again.\n";
    	}
    
    	system ("pause");
    		return 0;
    }
    does that seem right?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Nope. The whole point of enum is to eliminate "magic numbers" in your code, like 1, or 2, or 3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  2. Enumeration
    By webren in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 03:47 PM
  3. Printer enumeration problem
    By knutso in forum Windows Programming
    Replies: 7
    Last Post: 09-21-2004, 03:23 AM
  4. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 09:59 AM
  5. Enumeration in C and C++
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-27-2001, 02:12 PM

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