Hi GL SAM,
I think I am understanding you wrong... By removing the default line out of the switch I get an error. '... main.c:118: error: 'default' label not within a switch statement...'
If this is what you meant? I paste the code which does not work and thereafter the one that does work...
Code:
int main(int argc, char** argv) {
int option;
menu();
printf("Enter option: ");
scanf("%d", &option);
while (option != 0) {
switch (option) {
case 1:printf("Create employee\n");
fill_staff(); // takes index from user
employee_create(); break;// fills an array with index above
case 2: printf("Enter employee data\n");
for (d = 1; d <= emp_num; d++)
employee_data();break; //enter data for the employees created
case 3:printf("Print employee list\n");
for (d = 1; d <= emp_num; d++)
print_employee(); break;//prints the array of employees
case 4:printf("Exit program: ");
option = 0;break;
}
default: printf("\n\n\nEnter option: "); //Return back to the original state and continue choosing options <<< THIS IS MOVED OUT OF THE SWITCH STATEMENT
scanf("%d", &option);break;
}
return (EXIT_SUCCESS);
}
If I, on the other side, remove the break; statements I get the desired result.... why is that? I paste the code that works below:
Code:
int main(int argc, char** argv) {
int option;
menu();
printf("Enter option: ");
scanf("%d", &option);
while (option != 0) {
switch (option) {
case 1:printf("Create employee\n");
fill_staff(); // takes index from user
employee_create(); // fills an array with index above
case 2: printf("Enter employee data\n");
for (d = 1; d <= emp_num; d++)
employee_data(); //enter data for the employees created
case 3:printf("Print employee list\n");
for (d = 1; d <= emp_num; d++)
print_employee(); //prints the array of employees
case 4:printf("Exit program: ");
option = 0;
default: printf("\n\n\nEnter option: "); //Return back to the original state and continue choosing options
scanf("%d", &option);
}
}
return (EXIT_SUCCESS);
}