944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3623
  • C++ RSS
Jul 15th, 2005
0

Menu Chooser Program

Expand Post »
I'm at the end of chapter 2 of Michael Dawson's Beginning C++ Game Programming. The first exercise at the end of the chapter, has me rewrite a program earlier in the chapter using enumerations. The program is just a simple choose your difficulty level. You enter a number that represents the level, and it prints what level you chose. So if you enter 1, a message says you chose easy. Here is the code for that program.

C++ Syntax (Toggle Plain Text)
  1. // Menu Chooser
  2. // Demonstrates the switch statement
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. cout << "Difficult Levels\n\n";
  11. cout << "1 - Easy\n";
  12. cout << "2 - Medium\n";
  13. cout << "3 - Hard\n";
  14.  
  15. int choice;
  16. cout << "Choose a difficulty level: ";
  17. cin >> choice;
  18.  
  19. switch (choice)
  20. {
  21. case 1:
  22. cout << "You chose easy.\n";
  23. break;
  24. case 2:
  25. cout << "You chose medium.\n";
  26. break;
  27. case 3:
  28. cout << "You chose hard.\n";
  29. break;
  30. default:
  31. cout << "You made an illegal choice.\n";
  32. }
  33.  
  34. return 0;
  35. }

Here is the program that I have tried to rewrite using enumerations.

C++ Syntax (Toggle Plain Text)
  1. // Menu Chooser 2
  2. // Menu chooser using enumerations
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. cout << "Difficulty levels\n";
  11. cout << "1 - Easy\n";
  12. cout << "2 - Medium\n";
  13. cout << "3 - Hard\n";
  14.  
  15. enum difficulty {Easy = 1, Medium, Hard};
  16. difficulty myDifficulty;
  17.  
  18. cout << "Please enter a difficulty level: ";
  19. cin >> myDifficulty;
  20.  
  21. if (myDifficulty = 1)
  22. cout << "You chose easy\n";
  23.  
  24. if (myDifficulty = 2)
  25. cout << "You chose medium\n";
  26.  
  27. if (myDifficulty = 3)
  28. cout << "You chose hard\n";
  29.  
  30. return 0;
  31. }

I guess I don't fully grasp the concept of enumerations. From the explanation in the book, it seems like enumerations are a way to create your own variable type. Instead of making a variable something like "int" or "long int" or "float", you assign it to whatever you create using the enum command.

My program creates a enum called difficulty. Then I assign the variable myDifficulty as a difficulty type. The program then checks what the user inputs using the cin command and it compares it to what is allowed. Then it prints whatever difficulty level you choose.

Unfortunately, it just isn't working out that way. Perhaps someone could better explain what an enumeration is, and tell me where I went wrong using it in the program I created. Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Decessus is offline Offline
13 posts
since Jul 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

Your probleme lies in
C++ Syntax (Toggle Plain Text)
  1. if (myDifficulty = 3)
You are only asigning the value 3 (or 1 or 2) to the myDiffculty, change the '=' too '==' and it shuld work.

You arent using the enum difficulty.... if you
change the testing to:
C++ Syntax (Toggle Plain Text)
  1. if (myDifficulty == Easy)
you are using the enum.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

Yeah, I caught that after I had already posted. I was hoping that would fix the problem. It didn't change though. What happens is the line "cin >> myDifficulty" gets highlighted and I get a warning. I don't know how to read the compile log very well, but it says that there is "no match" for that line.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Decessus is offline Offline
13 posts
since Jul 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

no match for 'operator>>' in 'std::cin >> myDifficulty' is the error I get, and it means that you havent declared anny overload for the >>. but use int for geting the choise, and use the enum for testing. cant think that the book want you to start overloading in chapter2
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

I'll try that out. One thing I noticed while I was trying to test the program was that it kept running the wrong program. I'd go to the command line and type in menu_chooser_2, and instead it would run menu_chooser. I noticed this when I changed the visiable options from "1 - Easy, 2 - Medium 3 - Hard" to "Easy, Medium, Hard." When I ran the program however, it still showed the 1 the 2 and the 3. Any idea what would cause this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Decessus is offline Offline
13 posts
since Jul 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

If you're using Visual C++:
Try using 'Clean' under the 'Build' menu, and checking your project workspace to make sure you're telling it to use the proper files. If your old files are still there, select them and hit delete. This isn't a deletion of the file; it just removes it from the active project, allowing you to recompile without earlier code.

To get to your workspace window, type Alt+0, or go to the views menu.


Oh, right: Also, make sure to add the appropriate files to the project if you don't have them right now. That's under the project menu.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

Quote originally posted by Drowzee ...
If you're using Visual C++:
Try using 'Clean' under the 'Build' menu, and checking your project workspace to make sure you're telling it to use the proper files. If your old files are still there, select them and hit delete. This isn't a deletion of the file; it just removes it from the active project, allowing you to recompile without earlier code.

To get to your workspace window, type Alt+0, or go to the views menu.


Oh, right: Also, make sure to add the appropriate files to the project if you don't have them right now. That's under the project menu.
Ahh, sorry, I should have mentioned what IDE I was using. I'm using Dev-C++.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Decessus is offline Offline
13 posts
since Jul 2005
Jul 15th, 2005
0

Re: Menu Chooser Program

it show the menu 1,2 and 3 becuse in
C++ Syntax (Toggle Plain Text)
  1. if (myDifficulty = 3)
you aint checking, you are asigning the value 3 to myDifficulty change it to
C++ Syntax (Toggle Plain Text)
  1. if (myDifficulty == 3)
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MFC File I/O Philosophy questions/clarifications
Next Thread in C++ Forum Timeline: serious crashing problem in Visual C++ 6.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC