What does this SWITCH error mean?

I get this error for my CASE 5/6/7/8.

I am sure this was something obvious, since it worked before I started adding extra function calls to CASE 4.

What does the error mean?

error: case label in the scope of the identifier with a changed type that does not contain the enclosed 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; 
+4
source share
3 answers
 student_record data_record[rec_cnt]; 

You cannot declare stuff inside the switch.

  • Do it in front of the switch
  • Do this in a block:

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

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

Create an additional 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; 
+1
source

In case 4: you declared a new variable, which then remains in the scope of the rest of the switch. This is true only for C99 and C ++, but usually generates a warning (and not an error). The solution is to add {...} around the body of the enclosure 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 usually use this form in all switch / case constructs, as it simplifies maintenance.

0
source

Source: https://habr.com/ru/post/1400814/


All Articles