13.7 – Designing menu system – Turbo C

by subbu on January 13, 2014

In any application it is a common routine to select a single option from multiple options using a menu system. Even Turbo C IDE has different menu options to select different options in order to perform different operations. Now we will see how we can develop our own menu system step by step.

First we will see how multiple options can be displayed through an array of options

char items[4][20]={" Manip           ",
                   " Biggest         ",
                   " Factorial       ",
                   " Exit            "};

Here “items” is an array of words/strings. items[0] gives “Manip”, items[1] gives “Biggest” and items[3] gives “Exit”.Here the selected item must be displayed in reverse video and un-selected items must be displayed in normal format

for(i=1;i<=4;i++)
{
 if(i==in)                                      /* in is selected item number */
 {
   textcolor(BLACK);                  /* setting reverse video */
   textbackground(WHITE);
 }
 else
 {
  textcolor(WHITE);                   /* setting normal video */
  textbackground(BLACK);
 }
 gotoxy(1,i);
 cprintf("%s",items[i-1]);                 /* printing menu item  */
}

Here “in” is the selected item number through arrow keys. The loop for i=1,2,3,4 prints every item with cprintf(“%s”,items[i-1]); in normal video but, prints selected item in reverse video

Say for example, if the selected item “in” is 2 then item[2-1] that is “Biggest” prints in reverse video and other items print in normal video.

ch=getch();
if(ch==0)
  ch=getch();
switch(ch)
{
 case 72:  /* selecting up arrow */
  if(in>1)
    in--;
  break;
 case 80:  /* selecting down arrow */
  if(in<4)
    in++;
  break;
 case 13: /* selecting an item by pressing enter */
  return in;
}

Here getch() reads the key and decrements the value of item number (in) by one if the upper arrow key (72) is pressed, increased by one if the down arrow key (80) is selected and confirms the selected option on selecting the enter key (13) by returning the selected item (in).

Putting all together:

Here the function menu() accepts the selected item number as argument and display the selected item in reverse video and all other items in normal video.

It also takes the arrow keys from user and moves the reverse video up and down and returns the selected item number (in) if enter key (13) is selected.

int menu(int in) /* prints menu and returns selected item number*/
{
 int i;
 char temp,ch;
 char items[4][20]={" Manip            ",
                    " Biggest          ",
                    " Factorial        ",
                    " Exit             "};
 /* defining active window */
 window(20,5,50,10);
 while(1)
 {
   for(i=1;i<=4;i++) /* to print menu items */
   {
     if(i==in)
     {
       /* setting reverse video */
        textcolor(BLACK);
        textbackground(WHITE);
     }
     else
     {
        /* setting normal video */
        textcolor(WHITE);
        textbackground(BLACK);
     }
   /* printing item */
   gotoxy(1,i);
   cprintf("%s",items[i-1]);
  }
  /* accepting a key */
  ch=getch();
  if(ch==0)
    ch=getch();
  switch(ch)
  {
    case 72:  /* selecting up arrow */
     if(in>1)
      in--;
     break;
    case 80:  /* selecting down arrow */
     if(in<4)
      in++;
     break;
   case 13: /* selecting an item by pressing enter */
     return in; /* returning selected item number */
  }
 }
}

The above function allows you to select an item and returns selected item number. We call proper module/function to perform the action according to the selected item and will display the menu again by calling the same function until the option exit is selected.

int x=menu(1);   /* showing menu for the first time */
while(1)
{
  window(20,12,50,20);
  if(x==1)           /* if fist menu item is selected  */
    manip();
  else if(x==2)    /* if second menu item is selected */
    biggest();
  else if(x==3)    /* if third menu item is selected */
    factorial();
  else if(x==4)    /* if the option exit is selected */
    goto out;       /* going out of loop */
  x=menu(x);
}
out:

Program:

#include<stdio.h>
#include<conio.h>
void manip();
void biggest();
void factorial();
int menu(int);
int main()
{
  int x=menu(1);
  while(1)
  {
    window(20,12,50,20);
    if(x==1)
      manip();
    else if(x==2)
      biggest();
    else if(x==3)
      factorial();
    else if(x==4)
      goto out;
    x=menu(x);
}
out:
return 0;
}
int menu(int in) /* prints menu and returns selected item number*/
{
 int i;
 char temp,ch;
 char items[4][20]={" Manip            ",
                    " Biggest          ",
                    " Factorial        ",
                    " Exit             "};
 window(20,5,50,10);
 while(1)
 {
   for(i=1;i<=4;i++)
   {
      if(i==in)
      {
        textcolor(BLACK);
        textbackground(WHITE);
      }
      else
      {
        textcolor(WHITE);
        textbackground(BLACK);
   }
  gotoxy(1,i);
  cprintf("%s",items[i-1]);
 }
 ch=getch();
 if(ch==0)
   ch=getch();
 switch(ch)
 {
   case 72:  /* selecting up arrow */
     if(in>1)
       in--;
     break;
   case 80:  /* selecting down arrow */
     if(in<4)
       in++;
     break;
   case 13:
     return in;
  }
 }
}
void manip()
{
  int x,y;
  clrscr();
  textcolor(WHITE);
  textbackground(BLACK);
  gotoxy(1,1);
  cprintf("First number:\n\r");
  scanf("%d",&x);
  gotoxy(1,3);
  cprintf("Second number:\n\r");
  scanf("%d",&y);
  gotoxy(1,5);
  cprintf("Sum %d",(x+y));
  gotoxy(1,6);
  cprintf("Substraction %d",x-y);
  gotoxy(1,7);
  cprintf("Product %d",x*y);
  gotoxy(1,8);
  cprintf("Division %f",(float)x/y);
}
void biggest()
{
 int x,y;
 clrscr();
 textcolor(WHITE);
 textbackground(BLACK);
 gotoxy(1,1);
 cprintf("First number:\n\r");
 scanf("%d",&x);
 gotoxy(1,3);
 cprintf("Second number:\n\r");
 scanf("%d",&y);
 gotoxy(1,5);
 if(x==y)
   cprintf("Equals");
 else if(x>y)
   cprintf("Biggest number %d",x);
 else if(x<y)
   cprintf("Biggest number %d",y);
}
void factorial()
{
 int n,i,fact;
 clrscr();
 textcolor(WHITE);
 textbackground(BLACK);
 gotoxy(1,1);
 cprintf("Enter any integer:\n\r");
 scanf("%d",&n);
 for(fact=i=1;i<n;i++)
    fact=fact*i;
 gotoxy(1,3);
 cprintf("Factorial %d",fact);
}

Output Screen shots:

Menu design in C language

Previous post:

Next post: