PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] Why is my code expecting a primary expression in a switch case before curly brackets?

 June 26, 2022     c++, compiler-errors, enums, switch-statement     No comments   

Issue

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.


Solution

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: {} .



Answered By - M.M
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing