Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ enum from char

Tags:

c++

enums

char

Ok, I'm new at C++. I got Bjarne's book, and I'm trying to follow the calculator code.

However, the compiler is spitting out an error about this section:

token_value get_token()
{
    char ch;

    do {        // skip whitespace except '\n'
        if(!std::cin.get(ch)) return curr_tok = END;
    } while (ch!='\n' && isspace(ch));

    switch (ch) {
        case ';':
        case '\n':
            std::cin >> WS;      // skip whitespace
            return curr_tok=PRINT;
        case '*':
        case '/':
        case '+':
        case '-':
        case '(':
        case ')':
        case '=':
            return curr_tok=ch;
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9': case '.':
            std::cin.putback(ch);
            std::cin >> number_value;
            return curr_tok=NUMBER;
        default:            // NAME, NAME=, or error
            if (isalpha(ch)) {
                char* p = name_string;
                *p++ = ch;
                while (std::cin.get(ch) && isalnum(ch)) *p++ = ch;
                std::cin.putback(ch);
                *p = 0;
                return curr_tok=NAME;
            }
            error("bad token");
            return curr_tok=PRINT;
}

The error it's spitting out is this:

calc.cpp:42: error: invalid conversion from ‘char’ to ‘token_value’

token_value is an enum that looks like:

enum token_value {
    NAME,       NUMBER,     END,
    PLUS='+',   MINUS='-',  MUL='*',  DIV='/',
    PRINT=';',  ASSIGN='=', LP='(',   RP=')'
};
token_value curr_tok;

My question is, how do I convert ch (from cin), to the associated enum value?

like image 919
Glen Solsberry Avatar asked Aug 19 '09 01:08

Glen Solsberry


People also ask

Can enum be a char?

Using the Code First of all, YES, we can assign an Enum to something else, a char !

What is typedef enum in C?

The typedef keyword is used to name user-defined objects. Structures often have to be declared multiple times in the code. Without defining them using typedef each declaration would need to start with the struct / enum keyword, which makes the code quite overloaded for readability.

How do you declare an enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.

What is the size of enum in C?

The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.


1 Answers

You can't implicitly cast from char to an enum - you have to do it explicitly:

return curr_tok = static_cast<token_value> (ch);

But be careful! If none of your enum values match your char, then it'll be hard to use the result :)

like image 137
Jesse Beder Avatar answered Sep 23 '22 08:09

Jesse Beder