954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Menu Chooser Program

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.

// 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.

// 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.

Decessus
Newbie Poster
13 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Your probleme lies in

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:

if (myDifficulty == Easy)


you are using the enum.

zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

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.

Decessus
Newbie Poster
13 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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 :)

zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

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?

Decessus
Newbie Poster
13 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
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.

Ahh, sorry, I should have mentioned what IDE I was using. I'm using Dev-C++.

Decessus
Newbie Poster
13 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

it show the menu 1,2 and 3 becuse in

if (myDifficulty = 3)


you aint checking, you are asigning the value 3 to myDifficulty change it to

if (myDifficulty == 3)
zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You