issue with switch statement
HI there,
I implemented a menu with a switch statement with which one can navigate through the different options I offered, which coincide, in turn, with the cases on the switch statement.
I intended the line that contains 'default' for the menu to return to the initial state, that is where it asks me for the options I want to enter.
However instead of returning to the default state in which I can select the option that I want, the program terminates. Everything else works fine in the program... I thought about implementing a goto statement but this would be in violation of 'good programming practices' .... what can I do?
Also in order for the program to continue until the end of the process I removed the break; statements within the switch statement. Is this orthodox? I am thinking that perhaps I should have used if statements and do whiles... any suggestions?
Thank you
Code:
int main(int argc, char** argv) {
int option;
menu();
printf("Enter option: "); // this is the state in which I want the program when finishing <<<HERE
scanf("%d",&option);
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
default: printf("\n\n\nEnter option: "); //Return back to the original state and continue choosing options -- THIS IS WHERE THE PROBLEM IS <<<
scanf("%d",&option);
}
return (EXIT_SUCCESS);
}
issue with switch statement
Thank you guys...!! I will try the suggestion with the loop statement right away..
BR,
blue
probem with switch statement
I re-wrote the code to include the suggestion...However what I get now is that when I select an option it is permanently stuck in that option. If I select 'create employee' which corresponds to case 1 then it permanently asks me to enter employees. Did I do the while statement right? How do I move on to other cases? I post the updated code 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(); 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
scanf("%d",&option);break;
}
}
return (EXIT_SUCCESS);
}
issue with switch statement
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);
}
issue with switch statement
Quote:
Originally Posted by
madmax2006
Another minor thing I see is that you just say:
You want to set it equal to something other than zero. It may work 99% of the time, but that 1% of the time may be the 1% that your professor grades your assignment.
I take notice ;-)
issue with switch statement
Quote:
Originally Posted by
laserlight
You might want to consider something like this:
Code:
int option = menu();
while (option != OPTION_QUIT) {
switch (option) {
/* handle valid options */
default:
/* print error message concerning invalid option */
}
option = menu();
}
My idea is that handling the menu consists of two parts: printing it, and reading the menu option. Thus, menu() does both (possibly by calling other functions that handle the two individual parts), then returns the option read.
Now, instead of printing the menu in the default case, you print an error message. It is always the case that as long as the option is not an option to quit (i.e., the option matches the OPTION_QUIT constant), the menu will be printed and the next option read.
Thank you much laser light as a matter of fact thank you all!
This seems a more sophisticated way to make the menu...
I guess that for this I would have to have two functions one to read and one to print... and perhaps even some parameters right?
Blue