Home > Net >  Why is my code expecting a primary expression in a switch case before curly brackets?
Why is my code expecting a primary expression in a switch case before curly brackets?

Time:11-22

I am trying to use a switch case as a sort of menu selection for the user in my code. I have this enum list:

enum menuChoice {ADD = 1, REMOVE = 2, DISPLAY = 3, SEARCH = 4, RESULTS = 5, QUIT = 6};

Then I have this code:

        menuChoice switchChoice;
        Student student;

        cout << "1. Add\n" << "2. Remove\n" << "3. Display\n" << "4. Search\n";
        cout << "5. Results\n" << "6. Quit" << endl;

        for (int i = 0; i < 999;   i)
        {
                cout << "Please enter choice:";

                cin >> userChoice;

                if (userChoice > 6 || userChoice < 1)
                {
                        cout << "Incorrect choice. Please enter again" << endl;
                }
                else
                {
                        break;
                }
        }

        switchChoice = static_cast<menuChoice>(userChoice);
        switch(switchChoice){

        case 1:
                add_Student(student);

                break;
        case 2:

                break;
        case 3:

                break;
        case 4:

                break;
        case 5:

                break;
        case 6:
                break;

        default:
        }

Which is kicking back this error:

 error: expected primary-expression before ‘}’ token

I am really scratching my head over this. What is the mistake here? How am I not implementing a primary expression? I know that you aren't supposed to pass types to switch parameters but this is an enum variable I'm passing. Some help on this would be greatly appreciated.

CodePudding user response:

default: } is a syntax error. The default label must be followed by a statement or a block. (This applies to any othe sort of label too).

For example it could be default: break; or default: ; or default: {} .

  • Related