| | |
Menu Chooser Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
Here is the program that I have tried to rewrite using enumerations.
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.
C++ Syntax (Toggle Plain Text)
// Menu Chooser // Demonstrates the switch statement #include <iostream> using namespace std; int main() { cout << "Difficult Levels\n\n"; cout << "1 - Easy\n"; cout << "2 - Medium\n"; cout << "3 - Hard\n"; int choice; cout << "Choose a difficulty level: "; cin >> choice; switch (choice) { case 1: cout << "You chose easy.\n"; break; case 2: cout << "You chose medium.\n"; break; case 3: cout << "You chose hard.\n"; break; default: cout << "You made an illegal choice.\n"; } return 0; }
Here is the program that I have tried to rewrite using enumerations.
C++ Syntax (Toggle Plain Text)
// Menu Chooser 2 // Menu chooser using enumerations #include <iostream> using namespace std; int main() { cout << "Difficulty levels\n"; cout << "1 - Easy\n"; cout << "2 - Medium\n"; cout << "3 - Hard\n"; enum difficulty {Easy = 1, Medium, Hard}; difficulty myDifficulty; cout << "Please enter a difficulty level: "; cin >> myDifficulty; if (myDifficulty = 1) cout << "You chose easy\n"; if (myDifficulty = 2) cout << "You chose medium\n"; if (myDifficulty = 3) cout << "You chose hard\n"; return 0; }
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.
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
Your probleme lies in 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: you are using the enum.
C++ Syntax (Toggle Plain Text)
if (myDifficulty = 3)
You arent using the enum difficulty.... if you
change the testing to:
C++ Syntax (Toggle Plain Text)
if (myDifficulty == Easy)
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.
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?
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
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.
•
•
•
•
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.
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
it show the menu 1,2 and 3 becuse in you aint checking, you are asigning the value 3 to myDifficulty change it to
C++ Syntax (Toggle Plain Text)
if (myDifficulty = 3)
C++ Syntax (Toggle Plain Text)
if (myDifficulty == 3)
![]() |
Similar Threads
- menu-based program (Java)
Other Threads in the C++ Forum
- Previous Thread: MFC File I/O Philosophy questions/clarifications
- Next Thread: serious crashing problem in Visual C++ 6.0
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





