0
vector<string> cmdV = split(_cmd, " ");

switch (cmdV[0])
{

}

For some reason it gives me the error: "The value must have an integral or enum type" in cmdV[0]

The rigjh
  • 67
  • 1
  • 7

2 Answers2

2

switch statements require integral types on both the condition and the case statements (the latter of which must be known at compile-time). If you're making conditions based on the contents of a string, you must use something else, such as an if statement.

If you have to do a lot of conditional programming based on your strings, you might consider hashing your strings into integers. If done right, this can allow you to use switch statements in a switch faster than full string compares can be done. I made a compile-time version of the SpookyHash algorithm for this exact purpose.

2

If you really want to make a switch statement with values from a vector<string> you can use either atoi() or std::stoi(). More described in this answer: How can I convert a std::string to int?.

The following code should work:

#include <iostream>
#include <vector>
#include <string>

int main() {

    std::vector<std::string> test;

    test.push_back("2");

    switch(std::stoi(test[0])) {
        case 1:
            std::cout << "IT WORKS!";
            break;
        case 2:
            std::cout << "IT WORKS 2!";
            break;     
    }
}

It should print "IT WORKS 2!" without errors.

Or if you would want a string such as std::string str = "Test" to be one of the cases you could use the answer in this previous question:

enum string_code {
    eFred,
    eBarney,
    eWilma,
    eBetty,
    ...
};

string_code hashit (std::string const& inString) {
    if (inString == "Fred") return eFred;
    if (inString == "Barney") return eBarney;
    ...
}

void foo() {
    switch (hashit(stringValue)) {
    case eFred:
        ...
    case eBarney:
        ...
    }
}

NOTE: You should read a bit and make sure you understand the errors that might occur if you do this. And also read about different data types or else errors like this might occur: wrong result of casting string to int C++

EDIT: As mentioned in the comment below by @theOtherMichael the code above is not a "real" hashing method. But as found in this post there is a way to use the same idea but actually hash the values.

constexpr unsigned int hash(const char *s, int off = 0) {                        
    return !s[off] ? 5381 : (hash(s, off+1)*33) ^ s[off];                           
}                                                                                

switch( hash(str) ){
    case hash("one") : // do something
    case hash("two") : // do something
}

What can happen here though is that different strings might return the same value but it should work.

darclander
  • 1,526
  • 1
  • 13
  • 35
  • 1
    Since this question is a frequent one for beginners, I feel it's important to clarify that in the code example above, `hashit()` is _not actually_ a hash function, but a stand-in for one. If you want to hash a string for use in a `switch` statement, you will need to replace `hashit()` with an actual hashing algorithm: otherwise, you may as well write `if` statements. – theOtherMichael Dec 04 '20 at 00:11
  • 1
    @theOtherMichael I added another answer from the same post which should also work! – darclander Dec 04 '20 at 12:31