2

Assignment"Rewrite the menu chooser program from chapter using an enumerator to represent difficulty levels. the variable choice will still be of type int."

The first set of code is the original menu chooser program in its original untainted form. The second set of code is what I added to it in order to complete the assignment.

The only thing I want to ask is: Did I complete my assignment correctly. If I did it wrong, can someone please explain what I did wrong. I'm very new at this.

Code Set # 1 - Original

#include <iostream>
using namespace std;
int main()
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
    case 1:
            cout << "You picked Easy.\n";
            break;
    case 2:
            cout << "You picked Normal.\n";
            break;
    case 3:
            cout << "You picked Hard.\n";
            break;
    default:
            cout << "You made an illegal choice.\n";
}

return 0;
}

Code Set # 2 - Assignment

#include <iostream>

using namespace std;

int main()
{
    cout << "Difficulty Levels\n\n";
    cout << "0 - Novice\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n";
    cout << "4 - Unbeatable\n\n";

enum {Novice = 0, Easy = 1, Normal = 2, Hard = 3, Unbeatable = 4};

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
    case 0:     
            cout << "You have picked Novice.\n";
            break;
    case 1:
            cout << "You picked Easy.\n";
            break;
    case 2:
            cout << "You picked Normal.\n";
            break;
    case 3:
            cout << "You picked Hard.\n";
            break;
    case 4: 
            cout << "You picked Unbeatable.\n";
            break;
            default:
            cout << "You made an illegal choice.\n";
}

return 0;
}
5
  • 2
    I don't think so. Since the enum you wrote is not being used anywhere, and the program still has the same logic as before, it will work even without your enumerator, so I think this won't be approved Commented Sep 13, 2013 at 7:58
  • Dam, I knew something was wrong. I compiled it and it ran without a problem so I couldn't tell if I did it right or not. Commented Sep 13, 2013 at 8:02
  • 2
    Change case labels to enumerations. Commented Sep 13, 2013 at 8:06
  • If you find one of the answers satisfactory accept it for next people with similar problem, so they could find their solutions easier. Just a tip for the future :) Commented Sep 13, 2013 at 8:24
  • thanks for the tip. I'll do just that. Commented Sep 13, 2013 at 8:46

1 Answer 1

3

I would do something like this:

#include <iostream>
using namespace std;

int main()
{
    cout << "Difficulty Levels\n\n";
    cout << "0 - Novice\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n";
    cout << "4 - Unbeatable\n\n";

    enum {NOVICE = 0, EASY = 1, NORMAL = 2, HARD = 3, UNBEATABLE = 4};

    int choice;
    cout << "Choice: ";
    cin >> choice;

    switch (choice) {
        case NOVICE:     
            cout << "You have picked Novice.\n";
            break;
        case EASY:
            cout << "You picked Easy.\n";
            break;
        case NORMAL:
            cout << "You picked Normal.\n";
            break;
        case HARD:
            cout << "You picked Hard.\n";
            break;
        case UNBEATABLE: 
            cout << "You picked Unbeatable.\n";
            break;
        default:
            cout << "You made an illegal choice.\n";
            break;
    }  
    return 0;
}

This way you're showing you're at least using your enum.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I didn't realize it was so simple. Thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.