# Thread: quick question about C Dos text menu pgm i was doing

1. ## quick question about C Dos text menu pgm i was doing

Hello all. I dont post much on here, but checked out the FAQs n things. I might look into that displaying graphics in dos matter ( dont know though, not tooooo important of an issue for a leisure program ).

As i said awhile back im creating a selection menu. It's devided into catagories. I want to know how to jump back to a certain spot in the code ) after running an outside exe.

example:

#include < stdio.h >

int main();
{
int mainmenu;
int submenu;

printf("Select a pgm\n\n1. defrag\n");
scanf("%d", &mainmenu);
switch(mainmenu)
{
case 1:
system("c:\\windows\\defrag.exe");
system("menu.exe"); /* open menu */
break;

default:
printf("Invalid selection");
break;
}
{
printf("Choose a pgm\n\n1. scandisk\n2. clean");
scanf("%d", &submenu);
switch(submenu)

case 1:
system("c:\\windows\\scandskw");
system(" THIS IS WHAT I NEED ");
break;

case 2:
system("c:\\windows\\diskclean");
system(" THIS IS WHAT I NEED ");
break;
}
return 0;
}

This was a very basic example as i dont want to load up this post with miles of code. Sorry for the long post. You see though, I have a main menu, this menu allows you to choose a program, or, go into a second menu with several programs on it. If the user chooses a pgm from the 2nd menu, after that pgm is done running i want to return them to the 2nd menu and not the entire beginning.

The pgm im working on is much larger, but i just need the basic idea and then i can put it in.

2. O.K. this is an abstraction, so it doesn't exactly extrapolate to your exact program, but look at these two functions. Now unless your saying that the defrag.exe won't allow you to return to the console screen(if so, seek other's help), then this should work.
It appears to me you have not made each menu it's own function and I think you'd benifit to do so...

PHP Code:
 void main_menu(void) { int main_menu_choice=0; printf("1)Do defrag/scandisc/other operations \n"           "2)Set up user info                                 \n"           "3)Search Database                               \n"           "4)Exit                                                 \n\n" ); scanf("%d", &main_menu_choice); switch(main_menu_choice) { case 1: sub_menu1(); break; case 2: sub_menu2(); break; case 3: sub_menu4(); break; default: exit(1); } }  //End of main_menu function   void sub_menu1(void) { int sub_menu_choice=0; printf("1)Do defrag                                           \n"           "2)Do scandisc                                       \n"           "3)Exit                                               \n\n" ); scanf("%d", &sub_menu_choice); switch(sub_menu_choice) { case 1: system("c:\\windows\\defrag.exe");              main_menu();  //<----should return you to the main menu              break; case 2: system("c:\\windows\\scandisc.exe");              main_menu();              break; default: exit(1); } }  //End of sub_menu function  
In other words, getting to a menu is as simple as calling it by the function which encapsulates it. Note also that the users choice variable is declared within the function, not in main. Note also that after encapsulating all of your menus, main looks more like this:

PHP Code:
 #include <stdio.h> #include <stdlib.h> //function prototypes go here int main( main_menu(); printf("Goodbye!"); return 0; } /// define functions here  

This of course simplifies things since calling "main_menu()" you are also providing the means to get to the other menus specified in it's "switch/case" statement...

I hope that helps a little.

3. Bravo, Sebastiani

I agree with 100% of what he said. I think that is what Shadow was trying to accomplish.

Popular pages Recent additions