Are you able to have multi-character switch statement cases?

So if this is my code:

#include <iostream>

using namespace std;

int main(){
    char choice;
    cout << "Enter a word >" << endl;
    cin >> choice;
    switch (choice) {
        case 'ball':
            cout << "You entered ball" << endl;
            break;
        case 'bike':
            cout << "You entered bike" << endl;
            break;
    }
}

Am I able to run it? If so, will it work?

I actually am trying to run this code, but I can't. It will run, but then if I type in bike, it won't display "You entered bike". Is something wrong with the code?

The problem is that there is no such thing as a "multi-character" type. Something like 'ball' is not valid in C++ (although compilers must accept it, for some odd reason, but the value that the compiler gives to them is not defined). And because "choice" is a character, it will only read one character from the input stream (cin). That is why the whole thing will not work, you are capturing one letter from cin and then comparing it to two things that have undefined values.

I think that what you want is a "string", which is an array of characters (i.e., can represent words). In C++, the standard thing to use to represent strings is std::string from the header <string>. As so:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string choice;
    cout << "Enter a word >" << endl;
    cin >> choice;
    if(choice == "ball") {
        cout << "You entered ball" << endl;
    } else if(choice == "bike") {
        cout << "You entered bike" << endl;
    }
}

Notice that I did not use a switch-statement. This is because strings are more complicated than numbers and cannot be used for switch-statements (they only work for integral number types, like int, char, std::size_t, etc.).

One way is to encode the strings into an array and pass the index to the switch. However, it would not be particularly efficient unless the array is sorted and you can use a bsearch() to find the index. This is good for reasonably large data sets, but not for small ones.

FWIW, that approach can be useful for compiler lexical analyzers to determine what to do when a keyword is found.

AFAIK, for things like compiler lexical analyzers, they use hash tables. This is certainly an option here too (it's even possible to use a hash function in a switch-statement, thanks to constexpr (C++11)).

Another possibility is to use an associative container like std::map<std::string, int> to associate an integer to each string and to a switch based on those integer values.

Another neat little trick is to use a map of lambdas (C++11):

#include <iostream>
#include <string>
#include <functional>
#include <unordered_map>

using namespace std;

int main() {

    string choice;
    cout << "Enter a word >" << endl;
    cin >> choice;

    unordered_map< string, function< void() > > cases;

    cases["ball"] = []() {
      cout << "You entered ball" << endl;
    };
    cases["bike"] = []() {
      cout << "You entered bike" << endl;
    };

    cases[choice]();

}

But I would seem to me that the OP is a bit too early on the learning curve to start doing this kind of stuff, for his basic example, the if-else-if solution with strings is what seems most appropriate.

Yes Mike, I am quite early in the learning curve. Thank you guys for your help.

My last, simple, question would be: Can I use the string data-type for the switch statement or is it limited to the if-else statement?

Thank you, again, for your help.

By the way, what's weird is that, my original code worked after I posted this. Then it stopped working.

Can I use the string data-type for the switch statement or is it limited to the if-else statement?

No. You cannot use string data-type for a switch statement (unless using a special advanced trick). You have to stick with if-else statements.

By the way, what's weird is that, my original code worked after I posted this. Then it stopped working.

Your original code was valid C++ (as in, it can compile), however, the code that the compiler would produce for it is undefined (well, actually, it is implementation-defined), and certainly not what you'd expect it would do.

Again, Mike saves the day! :D Thank you to everyone who helped, by the way.

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.