1
    switch(choice)
    {
        case 1:
            uinstance1.addNewProduct(data);
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            uinstance1.listAllProducts(data);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:

            //name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
            //  Perishable(string,string,string,double,string,int,int);
            Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);

            data.addNew(item0);

            break;
        default:
            cout<<"Wrong Choice "<<endl;
            system("pause");
            break;
    }
}

Hi ,i have been thinking about this error for quite some time and cant seem to figure out the issue.

error C2361: initialization of 'item0' is skipped by 'default' label : see declaration of 'item0'

Some help would be appreciated. Thanks

user1203499
  • 267
  • 1
  • 7
  • 12

5 Answers5

7

The whole select block counts as one scope, if you decalare a variable in that scope you need to initialize it in every case statement (every possible execution path). You can avoid it by creating a additional scope in your case to avoid the problem (see the brackets):

switch(choice)
    {
        case 1:
            uinstance1.addNewProduct(data);
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            uinstance1.listAllProducts(data);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            {
                //name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
                //  Perishable(string,string,string,double,string,int,int);
                Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);

                data.addNew(item0);
            }
            break;
        default:
            cout<<"Wrong Choice "<<endl;
            system("pause");
            break;
    }
}
Fox32
  • 13,126
  • 9
  • 50
  • 71
3

MSDN explains the error C2361 aptly:

The initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)

Always pay attention to the error numbers they provide vital information about why the error.

You forgot the braces in one of the cases.

   case 10:
    {  
   ^^^
       Perishable item0;
       data.addNew(item0);

       break;
    }  
   ^^^ 
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Your label is crossing initialization which is illegal. Pretty sure moving default to the top should fix it. You can also add code blocks { } around your relevant code. If you still have problems then move your object outside of the switch block.

Pubby
  • 51,882
  • 13
  • 139
  • 180
1

You can't create variables inside case statements if you don't define explicity the scope.

There is another discussion about that: Variables inside case statement

Community
  • 1
  • 1
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
0
case 10:
{ // <<-- This gives explicit scope for the stack variable and let's you get rid of the error
  Perishable item0;
  // ...
}
break;
Roman R.
  • 68,205
  • 6
  • 94
  • 158