RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1941 | Replies: 7
Reply
Join Date: Jul 2005
Posts: 13
Reputation: Decessus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Decessus's Avatar
Decessus Decessus is offline Offline
Newbie Poster

Menu Chooser Program

  #1  
Jul 15th, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Menu Chooser Program

  #2  
Jul 15th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 13
Reputation: Decessus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Decessus's Avatar
Decessus Decessus is offline Offline
Newbie Poster

Re: Menu Chooser Program

  #3  
Jul 15th, 2005
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.
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Menu Chooser Program

  #4  
Jul 15th, 2005
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
Reply With Quote  
Join Date: Jul 2005
Posts: 13
Reputation: Decessus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Decessus's Avatar
Decessus Decessus is offline Offline
Newbie Poster

Re: Menu Chooser Program

  #5  
Jul 15th, 2005
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?
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Menu Chooser Program

  #6  
Jul 15th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 13
Reputation: Decessus is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Decessus's Avatar
Decessus Decessus is offline Offline
Newbie Poster

Re: Menu Chooser Program

  #7  
Jul 15th, 2005
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++.
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Menu Chooser Program

  #8  
Jul 15th, 2005
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)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:08 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC