Chef Flambe
Chef Flambe

Reputation: 883

What does this SWITCH error mean?

I'm getting this error for my CASE 5/6/7/8.

I'm sure it's something obvious as it was working before I started adding addition function calls to CASE 4.

What does the error mean?

error: case label in scope of identifier with variably modified type not containing enclosing switch statement

 switch(menu_selection())
    {
        case 0 :    i = find_empty_record(data_record);       //New record
                    if (i!=-99)
                    {
                        printf("\n\nRecord #%d found to be empty...\n\n",i);
                        data_entry(&data_record[i],i,&array_flag);
                    }
        break;

        case 1  :                                             //Edit
                i=record_selection(array_flag);
                data_entry(&data_record[i],i,&array_flag);
        break;

        case 2  :   display_single(data_record,array_flag);  //Display single record

        break;

        case 3  :                                           //Display all records
                for (i=0;i<30;i++)
                    {
                        print_2_screen(&data_record[i],i,array_flag);
                    }
        break;

        case 4  :   rec_cnt = get_text_file_size(import_file_name); //Import Text File
                    student_record data_record[rec_cnt];
                    import_text_file(data_record,import_file_name,array_flag,rec_cnt);
        break;


        case 5  :   //  Import Binary File
        break;


        case 6  :
                export_text(data_record,rec_cnt,array_flag);//  Save to Text File
        break;

        case 7  :   //  Save to Binary File
        break;

        default :
        break;
    }

}
return 0;

Upvotes: 5

Views: 4691

Answers (3)

cnicutar
cnicutar

Reputation: 182649

student_record data_record[rec_cnt];

You can't declare stuff inside a switch.

  • Do it before the switch
  • Do it in a block:

    case 4:
    {
        student_record data_record[rec_cnt];
        /* ... */
    }
    

Upvotes: 9

Clifford
Clifford

Reputation: 93486

In case 4: you have declared a new variable which then remains in scope throughout the rest of the switch. This is only valid in C99 and C++, but will usually generate a warning (not an error) even then. The solution is to add {...} around the case body to limit the scope of any declared variables:

    case 4  :
    {
      rec_cnt = get_text_file_size(import_file_name); //Import Text File
                student_record data_record[rec_cnt];
                import_text_file(data_record,import_file_name,array_flag,rec_cnt);
    }
    break;

Personally I habitually use this form in all switch/case constructs since it makes maintenance simpler.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96266

In case 4 you have an array declaration: student_record data_record[rec_cnt];

Create an extra block:

case 4:   
  {
    rec_cnt = get_text_file_size(import_file_name); //Import Text File
    student_record data_record[rec_cnt];
    import_text_file(data_record,import_file_name,array_flag,rec_cnt);
  }
  break;

Upvotes: 1

Related Questions