Adib Hasib
Adib Hasib

Reputation: 92

Simple menu in c

I am a new computer science student and I am currently working on a university project on super shop management system.

For that, I want a menu system. I tried using switch case.
It works but, for example, when I press 1 program goes to another function defined by me.

But main menu function still appears on the top.
I want a menu where i will press a number and program will proceed to a certain function clearing the previous commands I used.

Any help would be appreciated. Thank you.

(I tried using switch case. And I am newbie and don't know anything about file handling and that kind of stuff)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>


//User defined function declaration

int main_menu();
int products();
double payment();
int customer();

//User defined sub menus



//under products

int home_products();
int electronics();

int add_products();
int remove_products();
int products_quantity();

//Under payment

double billing();

//under customer menu()

int customer_inf();
int edit_customer();




int main ()
{
int input;

printf("                              Hello ADMIN\n                          SHIRONAMHIN Super Shop\n================================================================================");
printf("                              -Products\n                              -Payment/Billing\n                              -Customer information\n");
printf("\n");
printf(" Press One for Products \n Press two for Billing \n Press Three for Customer information \n Press 0 to terminate program");
scanf("\n%d", &input);
if(input==0)
{
    return 0;  //Program will be terminated if 0 is pressed
}
switch(input)
{
case 1:
    if(input==1)
    {
        products();
    }
    break;
case 2:
    if(input==2)
    {
        payment();
    }
    break;
case 3:
    if(input==3)
    {
        customer();
    }
    break;

default:
    printf("Enter a valid number");
    break;
}
 }


  int products()

  {
    int input;
printf("\n                              -Home products\n                              -Electronics\n\n Press any key rather than 1 and 2 two return to main menu");

scanf("%d", &input);

switch (input)
{
case 1:
    if(input == 1)
        home_products();
    break;

case 2:
    if(input == 2)
        electronics();
    break;

default:
    main();
    break;
}

   }

   int home_products()
     {
      int input;
         printf("Code yet to be written\n\n return to main menu by pressing 1 on the keyboard");
    scanf("%d", &input);
      if(input == 1)
       {
         return main();
     }


      }

   int electronics()
       {
      printf("Code yet to be written");
        }

      double payment()
      {

        return main();

      }

      int customer()
          {
     printf("Code yet to be written");

      return main();
          }

Upvotes: 3

Views: 17444

Answers (1)

Suraj Jain
Suraj Jain

Reputation: 4536

You Can Take A Look At This Answer. I Have Tested It On Windows.

Basically It Says

A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):

You Can Have A Function Like This

void clrscr()
{
    system("cls||clear");
}

Basically Issue With system("cls")(Works On Windows) is that it is not portable and same goes for system("clear")(Works On Linux).

Above Function Works On Both.

You Can Have It Like This

case 1:
    if(input==1)
    {
        clrscr();
        products();
    }

So If input == 1, first clrscr() will be called and console will be cleared and then products() will be called. You Can Do Same With all Other Function, do not forget to include the definition of the function clrscr() above.

Also I Highly Recommend You to take a look at This Question How do you clear the console screen in C? For more information on this topic.

Upvotes: 3

Related Questions