Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,064 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




Efficient use of enumerators

 
Reply to this topicStart new topic

Efficient use of enumerators

nexus
21 Apr, 2008 - 08:21 AM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 6


My Contributions
I'm learning C++ and I've done the following code using enumerators. My question is, is there a better way to write this, with less code while still using enumerators?

CODE

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Difficulty Levels\n\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n\n";
    
    enum difficulty {EASY, NORMAL, HARD};
    difficulty myDifficulty;

    int choice;
    cout << "Choice: ";
    cin >> choice;
    
    switch (choice)
    {
                case 1:
                          myDifficulty = EASY; break;
                case 2:
                          myDifficulty = NORMAL; break;
                case 3:
                          myDifficulty = HARD; break;
                default:
                          cout << "You made an illegal choice.\n";
             main();
                }

    switch (myDifficulty)
    {
    case EASY:    
            cout << "You picked Easy.\n";
            break;
    case NORMAL:      
            cout << "You picked Normal.\n";
            break;
    case HARD:    
            cout << "You picked Hard.\n";
            break;
    }

    return 0;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Efficient Use Of Enumerators
21 Apr, 2008 - 10:12 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
ok, first off... you call main() recursively. Don't. There are some very odd ball places where it is useful (usually has to do with having very small memory footprints). but in general it is a bad practice. Use loops, that is what they are made for.

Next. Enumerations are neat. People do often use switch-case structures to convert user input into an enumerated value, but you don't have to.

You are asking the user to input a number -- enumerations are numbers... so you can just jump from one to the other if you like.

Another note (this one just stylistic). People usually try to give types a capital first letter to distinguish them from variables (which generally start in lower case). Now the compiler does not care, but I bet you will find that it helps to make your code more readable if you name structs, enums, unions, classes, typedefs etc with an initial capital letter. Also name macro's in all upper case (if you ever use them).

So here might be one version of your program:
cpp
#include <iostream>
#include <limits>
#include <string>

using namespace std;

typedef enum {
EASY = 1,
NORMAL,
HARD
} Difficulty;

string settings[] = { "", "Easy", "Normal", "Hard" };

int main() {
Difficulty myDiff;
int userInput;
cout << "Difficulty Levels\n\n"
<< "1 - Easy\n"
<< "2 - Normal\n"
<< "3 - Hard\n\n"
<< "Choice (1-3): ";
cin >> userInput;
//we ant to make sure the user input a number, and it was in the correct range.
while (!cin || (userInput < 1 || userInput > 3)) {
cout << "\nBad input: Choice (1, 2, or 3):";
cin.clear(); //clear the error if user entered something like "abc"
cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); //clear any newline characters that might be hanging around....
cin >> userInput; //get the users' next try...
}
myDiff = (Difficulty)userInput; //we can cast from the int into the enum
cout << "You have chosen: " << settings[myDiff] << endl;
return 0;
}


for more on using enumerations see the bibliography at the end of my tutorial
User is offlineProfile CardPM
+Quote Post

nexus
RE: Efficient Use Of Enumerators
21 Apr, 2008 - 10:37 AM
Post #3

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 6


My Contributions
Thanks alot for the help and the pointers.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Efficient Use Of Enumerators
21 Apr, 2008 - 12:58 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
edit: nvm moot point

This post has been edited by KYA: 21 Apr, 2008 - 12:59 PM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:40PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month