-4

NOTE: my question is not duplicate of How to convert an enum type variable to a string? . In that solution we give computer one of enum values but I want ot give it an int value and get its related value like below.

I have an enum and I want to give it an int number and get its value like below:

int main() {

int choice;
string level;

enum difficulty
{
    Easy = 1,
    Normal,
    Hard
};

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

now if I enter 2 for choice, I expect to get "Normal" as an string. Something like:

string level = difficulty(choice); //choice = 2
cout << level;    //output = Normal

EDIT: I'm not allowed to use arrays.

myregm
  • 67
  • 1
  • 8
  • 1
    How it's not a duplicate? `enum` values always have associated integer value. If it is not specified explicitly (like in the linked question): it defaults to start at `0`, and increase by `1` to all subsequent values. As far as I can see it: you are asking how to do the thing, that is explained in the link you provided. – Algirdas Preidžius Dec 10 '19 at 20:00
  • 1
    "*In that solution we give computer one of enum values but I want to give it an int value and get its related value like below.*" An enum value **is** an int value. So it's not clear how your question is different from the one you linked. – scohe001 Dec 10 '19 at 20:02
  • can you tell me how to do it here? i couldn't find my answer in linked question. – myregm Dec 10 '19 at 20:06
  • @myregm have you tried the solutions in the answers to that linked question? If so, why didn't they work for you? It looks like they should. I don't see you creating an array like they talk about. Have you tried that? Maybe you could show us that attempt and tell us what went wrong? – scohe001 Dec 10 '19 at 20:14
  • @scohe001 . it's for my school and I'm not allowed to use array like what they talk. is there any way to do it without array and only with enum? – myregm Dec 10 '19 at 20:20
  • @myregm If the answer is "There really is no beautiful way of doing this.", and you are looking for a beautiful way of doing this, that doesn't make the answer not an answer, because it isn't what you wanted, since what you want - is not possible. – Algirdas Preidžius Dec 10 '19 at 20:20
  • it is a duplicate. And here are more: https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20?noredirect=1&lq=1 https://stackoverflow.com/questions/201593/is-there-a-simple-way-to-convert-c-enum-to-string?noredirect=1&lq=1 – 463035818_is_not_an_ai Dec 10 '19 at 20:24
  • 2
    @Ivanovic "_now always downvotes on beginner questions_" We expect that people do their research before asking. Even the tooltip of the downvote button says "This question does not show any research effort". If you are a beginner, improving your researching (searching) skills is just as important (if not more), than improving actual development skills. – Algirdas Preidžius Dec 10 '19 at 20:26
  • @Algirdas Preidžius thanks. i was looking for this answer. now i know it's not possible. people could simply say it's not possible in this way insted of downvotes. – myregm Dec 10 '19 at 20:26
  • @Ivanovic the linked question didnt get its 111 ups in its first 30 minutes ;) – 463035818_is_not_an_ai Dec 10 '19 at 20:28
  • 1
    @myregm Giving us a problem to fix and leaving out restrictions on the solution like "can't use arrays" is why you're getting down votes. You need more detail and more focus, you're lucky the question isn't closed. – Bobby Tables Dec 10 '19 at 20:29

2 Answers2

2

If you really can't use an array (which would be by far the easiest way), you could build a function with a switch case:

enum difficulty
{
    Easy = 1,
    Normal,
    Hard
};

std::string diff_name(difficulty d) {
    switch(d) {
        case Easy: return "Easy";
        case Normal: return "Normal";
        case Hard: return "Hard";
        default: return std::string();
    }
}

And then in main you could do

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

std::cout << "You chose: " 
          << diff_name(static_cast<difficulty>(choice)) << std::endl;
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • 3
    @myregm You do realize that the question you linked has this solution as the top voted answer? It's getting downvotes because it should be closed as low effort. – Bobby Tables Dec 10 '19 at 20:38
  • Oh yikes. I didn't even see that @Bobby. I only read the accepted answer. I wouldn't have posted if I'd known, but now that it's accepted it's too late :/ – scohe001 Dec 10 '19 at 20:42
1

Enum variables don't store strings anywhere. You're essentially asking if you can print the literal variable name as in int test_var and you'd want it to print "test_var". There is nothing built in to do this.

What you need to do is have an array of strings and use the input choice as an index. You don't actually need an enum but if you want to use it for clarity it works? If you will only have 3 choices I don't see it being helpful.

string DifficultyNames[3]={"Easy, "Medium", "Hard"} and then you can just use DifficultyNames[choice-1](if you want input 1 indexed) You NEED to check the user input to make sure it's within the range 1-3

Edit: I just read in the comment that there's a restriction on the problem to not allow arrays.I'll leave the answer up though.

Bobby Tables
  • 191
  • 2
  • 15