C++

How to Fix ‘Jump to Case Label Crosses Initialization’ Error in C++

The switch statement is the best alternative for the long if-else-if statement, as it reduces the length of the code and brings better clarity. The value is evaluated using the switch statement and is tested against the values of each case label. If the match is found, the code executes before the break, otherwise, it executes the code after the “default”.

What Causes ‘Jump to Case Label Crosses Initialization’ Error in C++

While using the switch statement, in C++ a common error that may be observed is ‘Jump to Case Label Crosses Initialization’. This error usually appears due to the incorrect declaration of a variable within the case label. Here is an example program to demonstrate the ‘Jump to Case Label Crosses Initialization’ Error in C++:

#include <iostream>
#include <cmath>
using namespace std;        
void menu_display();
int choice_menu();
void get_two_operands(int &n, int &m);
int add(int n, int m);
int subtract(int n, int m);


int main()
 {
 int selection;

  do
   {
    menu_display();
    selection = choice_menu();
    int x, y;
    switch (selection)
    {
        case 1:
       
         get_two_operands(x, y);
                int sum = add(x, y);
                cout << x << " + " << y << " = " <<  sum << endl;
           
                break;
               
        case 2:
           
        get_two_operands(x, y);
       
                int diff = subtract(x, y);
                cout << x << " - " << y << " = " <<  diff << endl;
           
                break;
        default:;
    }

     } while (selection != 3);

     cout << "Program Terminated" << endl;

     return 0;
       }


 void menu_display()
  {
   cout << endl;
 cout << " Basic Calculator " << endl;
   cout << " 1. Add (+) " << endl;
   cout << " 2. Subtract (-) " << endl;
   cout << " 3. Exit" << endl;

   cout << endl;
  }

 int get_menu_choice()
  {
   int choice;
   cout << "Invalid Choice Try Again: ";
   cin >> selection;

  while(((selection < 1) || (selection > 3)) && (!cin.fail()))
   {
    cout << ": ";
    cin >> selection;
    }
  if (cin.fail())
    {
      cout << "Error " << endl;
      exit(1);
     }
   return selection;
    }

 void get_two_operands(int &n, int &m)
  {
    cout << "Give two operands " << endl;
     cout << "First Operand: ";
         cin >> n;
         cout << "Second Operand : ";
         cin >> m;

  }


 int add(int n, int m)
  {
   return (n + m);
  }

 int subtract(int n, int m)
  {
    return (n - m);
  }

 
When this program is executed, it returns a ‘Jump to Case Label’ error. This is a compilation error.

This error appears because of the declaration of the variable inside a case statement without the enclosing brackets. The case is just a label, so it does not have the ability to restrict the availability of the parameters written under it. When the above code is executed the variables of case 1 can be accessed by case 2, and it will appear as uninitialized leading to errors.

How to Fix ‘Jump to Case Label Crosses Initialization’ Error in C++

This error can be fixed by using brackets inside the case block, which will create an enclosed scope and the parameters of the different case blocks will not be manipulated.

Debugged Code

This is the debugged code for the above error. The error is fixed by enclosing the data under cases within the brackets:

#include <iostream>
#include <cmath>
using namespace std;        
void menu_display();
int choice_menu();
void get_two_operands(int &n, int &m);
int add(int n, int m);
int subtract(int n, int m);


int main()
 {
 int selection;

  do
   {
    menu_display();
    selection = choice_menu();
    int x, y;
    switch (selection)
    {
        case 1:
        {
         get_two_operands(x, y);
                int sum = add(x, y);
                cout << x << " + " << y << " = " <<  sum << endl;
            }
                break;
        case 2:
        {
          get_two_operands(x, y);
                int diff = subtract(x, y);
                cout << x << " - " << y << " = " <<  diff << endl;
            }
                break;
        default:;
    }

     } while (selection != 3);

     cout << "Program Terminated" << endl;

     return 0;
       }


 void menu_display()
  {
   cout << endl;
   cout << " Basic Calculator " << endl;
   cout << " 1. Add (+) " << endl;
   cout << " 2. Subtract (-) " << endl;
   cout << " 3. Exit" << endl;
   cout << endl;
  }

 int choice_menu()
  {
   int selection;
   cout << "Choose The Operation: ";
   cin >> selection;

  while(((selection < 1) || (selection > 3)) && (!cin.fail()))
   {
    cout << "Invalid Choice Try Again: ";
    cin >> selection;
    }
  if (cin.fail())
    {
      cout << "Error" << endl;
      exit(1);
     }
   return selection;
    }

 void get_two_operands(int &n, int &m)
  {
    cout << "Give two operands " << endl;
     cout << "First Operand: ";
         cin >> n;
         cout << "Second Operand : ";
         cin >> m;

  }


 int add(int n, int m)
  {
   return (n + m);
  }

 int subtract(int n, int m)
  {
    return (n - m);
  }

 
After the use of enclosing brackets in the case blocks of the code, the error has been fixed and the output is displayed:

Conclusion

The switch statement is the best alternative for the long if-else-if statement, as it reduces the length of the code and brings better clarity. While using the switch statement, in C++ a common compilation error that may be observed is ‘Jump to Case Label Crosses Initialization’. This error usually appears due to the incorrect declaration of a variable within the case label. This can be fixed by using enclosing brackets around the data under case blocks.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.