Why the switch statement cannot be applied on strings?

前端 未结 20 2982
离开以前
离开以前 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:37

        cout << "\nEnter word to select your choice\n"; 
        cout << "ex to exit program (0)\n";     
        cout << "m     to set month(1)\n";
        cout << "y     to set year(2)\n";
        cout << "rm     to return the month(4)\n";
        cout << "ry     to return year(5)\n";
        cout << "pc     to print the calendar for a month(6)\n";
        cout << "fdc      to print the first day of the month(1)\n";
        cin >> c;
        cout << endl;
        a = c.compare("ex") ?c.compare("m") ?c.compare("y") ? c.compare("rm")?c.compare("ry") ? c.compare("pc") ? c.compare("fdc") ? 7 : 6 :  5  : 4 : 3 : 2 : 1 : 0;
        switch (a)
        {
            case 0:
                return 1;
    
            case 1:                   ///m
            {
                cout << "enter month\n";
                cin >> c;
                cout << endl;
                myCalendar.setMonth(c);
                break;
            }
            case 2:
                cout << "Enter year(yyyy)\n";
                cin >> y;
                cout << endl;
                myCalendar.setYear(y);
                break;
            case 3:
                 myCalendar.getMonth();
                break;
            case 4:
                myCalendar.getYear();
            case 5:
                cout << "Enter month and year\n";
                cin >> c >> y;
                cout << endl;
                myCalendar.almanaq(c,y);
                break;
            case 6:
                break;
    
        }
    

提交回复
热议问题