Hi,

I'm trying to convert a string to an enum data type, and am having a bit of a hard time to do it.

Basically I have

enum MyType{
   Saw, Saw2, Saw3
};

more over I try to read from the console:

MyType m;

cout << "What's the movie name?" << endl;
cin >> m;

I want to force the possibility of having this string with alphanum data...
But can't seem to convert it to the enum:

Error 1 error C2440: 'type cast' : cannot convert from 'std::string' to 'MyType'


Oh right, I changed all those data types to include the info on the web. I really want to convert the string to an Enum :)

thanks,
Nuno

Recommended Answers

All 7 Replies

Enumerated types are constant integrals, they are created by the compiler, so you can't convert a string to an enumerated type.

Your enum

enum MyType{
   Saw, Saw2, Saw3
};

the compiler would assign these values.

Saw = 0, Saw2 = 1, Saw3 = 2

Yeah Gerard,

I know about that.
I just thought I could do some kind of a mapping from Saw2(string), to the value held by the enum with Saw2

Assuming that the enum are coordinates, instead of the movie names, with some math I will be able to do that mapping... I hope!

Thanks,
Nuno

what i would do is call a function that checks if the string compared to saw saw2 saw 3
and return the right value each time...
dont know of any other enum use in that context sry :)

Hi,

Just solved it with some string manip + math :) It is working as I expected :)

Case closed! :)

Thanks for all your help, though :)

Nuno

care to share what you did / mean?

Simple,

imagine a pair of coordinates x,y. If I wanted to calculate the position in the matrix for L51 I just had to process L as a char, and get its int value by subtracting with 'L', and to do the same to 51, multiplying by the equivalent number of cols/rows I want to calculate.

Hard for me to explain this, but it is working!

Usually I do this sort of string <-> enum mapping with an array. Example (using your stuff):

enum MyType{
   Saw, Saw2, Saw3, mt_end
};
const char* pMyType[] = { "saw", "saw2", "saw3" };

Now, you can use MyType as an index into the array pMyType. Assume the use input the name "saw2", then you could use something like this to determine which enum they referred to:

MyType findType(const char* user_input)
{
    for (MyType t = Saw; t != mt_end; t++)
    {
        if (pMyType[(int)t] == user_input)
        {
            return t;
        }
    }
    return mt_end;
}

This works because enums are either char or int types (depending upon what is something for your to look up). Just remember, you cannot assign an integer value to an enum without casting it to the enum type. Ie, this is wrong:

int i = 2; // 2 == saw3
MyType t = i; // Compiler error!

but this is not:

int i == 2; // 2 == saw3
MyType t = (MyType)i; // Ok, cast works, as long as 'i' associates with a member of MyType

Clear as mud, right! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.