menu for console
Primary tabs
int mainmenu(void)
{
int ch;
char* mtext = " Please specify the number of the task. \n * You can choose on number from set = {1,2} \n * Specify \"0\" to exit\n" ;
char* errmes = " Error(!) = Main menu does not support this command.\n Make sure that your task number is from menu set of commands and try again. " ;
fflush(stdin);/*here we are cleaning the input = fflush(stdin) isn't cool variant;or use while (getchar() != '\n'); - but this'll stop program*/
printf("\n%s",mtext );
ch = getchar() ; /*user choose an item*/
while (getchar() != '\n'); /*remove \n or other useless data*/
switch(ch)
{
case '1': printf("\n%s","you choise is item2 \n" ); /*menu item = just put here the name of your function instead of printf()*/
break;
case '2': printf("\n%s","you choise is item2 \n" ); /*menu item = here the name of your function instead of printf()*/
break;
default: printf("\n%s\n", errmes); /* inform about errror - if user sybmol isn't in menu list*/
}
mainmenu(); /* show menu again*/
return 0;
}
пример без рекурсии (более оптимален чем предыдущий) =
/*меню для второго задания (где надо создать три потока) -его выполним без рекурсии
- так как с каждым витком рекурсии программа начинает етсь всё больше и больше оперативной памяти*/
int task2menu(void)
{
int ch;
char* mtext = " This is TASK2 menu= \n \n * Specify \"1\" to run solution standard files for compare operations\n * Specify \"9\" to come back in main menu \n * Specify \"0\" to exit\n" ;
char* errmes = " Error(!) = Main menu does not support this command.\n Make sure that your task number is from menu set of commands and try again. " ;
inputcl();;/*here we are cleaning the input = fflush(stdin) isn't cool variant;or use while (getchar() != '\n'); - but this'll stop program*/
for (;;) // "бесконечный цикл"
{
printf("\n%s",mtext );
ch = getchar() ; /*user choose an item*/
switch(ch)
{
case '1': task2(); /* некое действие для первого элемента меню*/
break;
case '9': mainmenu();/*если "9" - можно , например, вызвать аналогичное главное меню*/
break;
case '0': exit(0); // в данном случае - завершение программы
default: printf("\n%s\n", errmes); /* inform about errror - if user sybmol isn't in menu list*/
}
}
return 0;
}
_____________________________________________
Источники(читать подробнее)=
меню консоль си
Ключевые слова и фразы(для поиска)=
меню консоль си
- vedro-compota's blog
- Log in to post comments
- 9813 reads
Comments
tony
Sun, 11/27/2011 - 17:50
Permalink
Ох
Всё хорошо, только:
vedro-compota
Wed, 01/18/2012 - 20:25
Permalink
действительно - из-за
действительно - из-за рекурсии с каждым витком начинает съедаться оператива . добавил альтернативный пример.
_____________
матфак вгу и остальная классика =)
vedro-compota
Thu, 12/01/2011 - 13:17
Permalink
да, рекурсия хуже....не знаю
да, рекурсия хуже....не знаю правда почему.
вместо
можно использовать процедуру типа =
function input_clearing() { printf("%s \n", "[If program is stopped - press ENTER to continue]e"); while (getchar() != '\n'); }_____________
матфак вгу и остальная классика =)