Why the switch statement cannot be applied on strings?

前端 未结 20 2561
离开以前
离开以前 2020-11-22 03:58

Compiling the following code and got the error of type illegal.

int main()
{
    // Compilation error - switch expression of type illegal
    sw         


        
20条回答
  •  北海茫月
    2020-11-22 04:26

    You can't use string in switch case.Only int & char are allowed. Instead you can try enum for representing the string and use it in the switch case block like

    enum MyString(raj,taj,aaj);
    

    Use it int the swich case statement.

提交回复
热议问题