You should have one overall do...while loop with all your other submenus nested inside with their own do..while loops. This way, when the menu selection has finished its processing, it will loop back to its parent menu (or in the case of the main menu, exit).
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Ehh.. It would perhaps be better to break-up the menu into separate functions.. A new function for each sub-menu.. which could then re-call the main menu at it's conclusion. That's just me, though.. As a java programmer, I love using my own methods :)
joshSCH
Industrious Poster
4,864 posts since Jul 2005
Reputation Points: 1,315
Solved Threads: 10
>so you're saying we should break the codes down to ndividual building blocks?
No, I'm not saying that, but it might be a good idea (and would help you see your program's design better).
Just for simplicity's sake, let's say you did break it down into functions. Here's how the overall main menu structure might look:
do
{
printf("Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
menu1();
break;
case 2:
menu2();
break;
case 3:
printf("End\n");
break;
}
} while (choice != 3);
Just to reinforce, here's what menu1() might look like:
void menu1()
{
do
{
printf("\n\t\tCHOOSE WHICH SECTION\n");
printf("\t1. Info\n");
printf("\t2. Calculation\n");
printf("\t3. End\n");
printf("Choice: ");
scanf("%s", &pilih);
switch(pilih){
case '1':
printf("Force = Mass * Acceleration\n");
printf("The unit is Newton\n");
break;
case '2':
printf("Force = Mass * Acceleration\n\n");
printf("Input Mass in kilogram: ");
scanf("%d", &mass);
printf("Input Acceleration in meter per second second: ");
scanf("%d", &accel);
force = mass * accel;
printf("Force is %d\n", force);
break;
case '3':
printf("End\n");
break;
default:
printf("Invalid section\n");
break;
}
} while(pilih !=1);
}
Notice I haven't bothered to deal with variables, but hopefully you can see what I'm getting at.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
You'd have two extra functions: menu1() and menu2() . Try it and see what happens.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339