c++switch-statement

C++ Switch Statement


Recently (yesterday hah) I started learning C++. I am trying to make a simple calculator to practice. I used a switch statement in order to call upon the correct method(or is it function.. don't know the nuances of c++...) within the class;

However, the code will not compile because I am using a string to define which case to use and also defining multiple classes to have the same result.

Here is the switch statement (I've only done addition to squash any bugs before I add the others):

switch(input){
        case 'A': case 'a': case 'add': case 'Add':
            cout << bo.addNum();
            break;
        default:
            cout << "Not addition";
            break;
    }

The error I'm getting is the following:

basic.cpp:41:2: error: statement requires expression of integer type ('string'
      (aka 'basic_string<char, char_traits<char>, allocator<char> >') invalid)
        switch(input){
        ^      ~~~~~
basic.cpp:42:28: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':
                                         ^
basic.cpp:42:40: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':
                                                     ^

Here is the code in its totality:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class beckahClass{
public:
    void setNum(int num1, int num2){
        a = num1;
        b = num2;
    }
    int addNum(){
        return a + b;
    }
    int subNum(){
        return a - b;
    }
    int divNum(){
        return a / b;
    }
    int multNum(){
        return a * b;
    }
private:
    int a, b;
};

int main (void){
    beckahClass bo;
    string input;
    int a, b;

    cout << "Please specify the operation to preform by the following:\n A: add\nS: subtract\nM: Multiple\nD: divide\nEnter operation:  ";
    cin >> input;

    cout << "Enter the two nums you want to preform your operation on: ";
    cin >> a >> b;
    bo.setNum(a, b);

    switch(input){
        case 'A': case 'a': case 'add': case 'Add':
            cout << bo.addNum();
            break;
        default:
            cout << "Not addition";
            break;
    }   
    return 0;
}

I also realize there are probably more efficient ways to use logic; because I'm just starting C++, I would greatly appreciate any critique. I had been looking up 'maps' before briefly and was wondering if this might be a good instance to use it in?

Thanks.


Solution

  • The reason is that C/C++ switch statement takes an int argument and do not support string as a type. Although it supports the notion of constant array. Also to mention that C/C++ switch statements are typically generated as branch tables. and it is not easy to generate a branch table using a string style switch.

    In C++ the switch statement takes int as the argument.

    Why you cannot string in switch and getting the below error?

    basic.cpp:42:28: warning: multi-character character constant [-Wmultichar]
                    case 'A': case 'a': case 'add': case 'Add':
                                             ^
    basic.cpp:42:40: warning: multi-character character constant [-Wmultichar]
                    case 'A': case 'a': case 'add': case 'Add':
    

    The reason is because to generate the code for switch the compiler must understand what it means for two values to be equal. For int and enum, it is trivial and easy as they are constant values. But when it comes to string then it is difficult resulting in error.