22

Possible Duplicate:
Why can't variables be declared in a switch statement?

I have a strange error in my code below:

char choice=Getchar();
switch(choice)
{
case 's':
    cout<<" display tree ";
    thetree->displaytree();
    break;

case 'i':
    cout<<"  enter value to insert "<<endl;
    cin>>value;
    thetree->insert(value);
    break;
case 'f' :
    cout<< "enter value to find ";
    cin>>value;
    int found=thetree->find(value);
    if(found!=-1)
        cout<<" found  =  "<<value<<endl;
        else
            cout<< " not found " <<value <<endl;
        break;
default:
    cout <<" invalid entry "<<endl;;
    }

Visual Studio 2010 compiler says that:

1>c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label
1>          c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(308) : see declaration of 'found'

I think that I have correctly written break and default statements, so where is the error?

Murphy
  • 3,827
  • 4
  • 21
  • 35
  • 8
    This is only an exact duplicate if you already know the answer to the problem. The cryptic "error C2361: initialization of 'found' is skipped by 'default' label" does not necessarily lead you to the question 'Why can't variables be declared in a switch statement?' – Tim MB Oct 17 '12 at 16:08
  • 13
    i got today the same problem :) i am not a c++ professional and i couldn't know that declaring a pointer in a 'case' without curly braces is not allowed. so just a thought, if you know the answer or solution and want to share it then please share, but stop acting here like a smart ass. – Constantin Mar 07 '13 at 15:29
  • @Constantin agree with 'smart ass' thing - but whom you are referencing to? :) – Alexander Sep 24 '19 at 12:23

3 Answers3

66

You need to either enclose your case 'f': with a scoped brace:

case 'f' :
{  
    cout<< "enter value to find ";
    cin>>value;
    int found=thetree->find(value);
    if(found!=-1)
        cout<<" found  =  "<<value<<endl;
    else
        cout<< " not found " <<value <<endl;
    break;
}

or place the declaration of found outside of the switch

Component 10
  • 10,247
  • 7
  • 47
  • 64
25

The semantics of a switch are those of a goto: cases don't introduce a new scope. So found is accessible in your default: case (although you don't actually access it). Jumping over a non-trivial initialization is illegal, so your code becomes illegal.

Given the complexity of your case 'f':, the best solution is probably to factor it out into a separate function. Failing that, you can put the entire case in {...}, creating a separate scope, or forgo the initialization, writing:

int found;
found = thetree->find(value);

(I mention this for completeness. It is not the solution I would recomment.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
7

You need to declare the internal variables of switch's case within curly braces. i.e.

case 'f' :
{
    ...
    int found=thetree->find(value);
    ...
}
iammilind
  • 68,093
  • 33
  • 169
  • 336