so basically i wrote a progarm that begins with a menu allowing you to select one of 5 functions, but at the end of whichever function i would like it to return to the menu
the problem is i dont know how to do that and would like any tips or if any of you would be so kind as to clue me in on how to do it
im fairly certain it has to do with the return statement but am not sure how
thank you

so basically i wrote a progarm that begins with a menu allowing you to select one of 5 functions, but at the end of whichever function i would like it to return to the menu
the problem is i dont know how to do that and would like any tips or if any of you would be so kind as to clue me in on how to do it
im fairly certain it has to do with the return statement but am not sure how
thank you

Hello, write a function that will include your menu and whenever you need your menu call this function..e.g.

#include <stdio.h>

void menu() { /*function that will retain your menu*/  }
void func() {
  /* a function that will do some work*/
  ...
  menu();  //whenever you need your menu call function menu()
}

 main() {
  func();
   
}

Or you could try something like this:

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

int main(void){
    while(1){
        system("cls");
        int opc;
        bool process=false;
        printf("Select an option:\n");
        printf("1. Option A:\n");
        printf("2. Option B:\n");
        printf("Option: ");
        scanf("%d",&opc);
        if(opc > 0 && opc < 3) process=true;
        if(process){
            //do something 
            printf("\n\nDOING SOMETHING!");
        }//else let it go back to the start :)
    }
    getchar();
}

You can use a simple switch ..
May be like this..

#include <stdio.h>
int main(void){
    while(1){
        int opc;
        printf("What u want to do?\n");
        printf("1. smthing\n");
        printf("2. smthing\n");
        printf("Your Choice: ");
        scanf("%d",&opc);
        switch(opc)
        {
          case 1 : FirstFunction();
            break;
          case 2 : SecondFunction();
            break;
           ..
           ..
           ..
          default: printf("Wrong Choice\n");
           break;
        }
    }
    getchar();
}

The while loop was going to be my suggestion, but the function call look petty useful too.

What I like to do is call menu() function from main. Menu() is just a BIG while(1) loop, with code to exit when the user requests it.

Inside the while loop, it prints the header or banner, the menu options, and has a big switch (choice) block of code, to handle all responses.

When the user wants to quit, menu() just returns to main(), which may have as little as two lines in it:

int main(void) {
  menu();
  return 0;
}

Edited 6 Years Ago by Adak: n/a

cut while loop from main and create menu() function paste it thr..

void menu()
  {
    while(1){
        int opc;
        printf("What u want to do?\n");
        printf("1. smthing\n");
        printf("2. smthing\n");
        printf("Your Choice: ");
        scanf("%d",&opc);

        switch(opc)
        {
          case 1 : FirstFunction();
            break;
          case 2 : SecondFunction();
            break;
           ..
           ..
           ..
          default: printf("Wrong Choice\n");
           break;
        }
     }
  }

I prefer to have menu do the menu only. No loops, no decisions. It returns the selection to the calling function where it is analyzed and acted upon.

Each function should do what the name implies, not a bunch of additional work.

Anyone testing going here?????

May be this is what you wanteed

int menu();

int main()
{
      while(1)
      {
         switch( menu() )
         {
          case 1 : FirstFunction();
            break;
          case 2 : SecondFunction();
            break;
           ..
           ..
           ..
          default: printf("Wrong Choice\n");
           break;
         }
      }
}

int menu()
  {
        int opc;
        printf("What u want to do?\n");
        printf("1. smthing\n");
        printf("2. smthing\n");
        printf("Your Choice: ");
        scanf("%d",&opc);
        return(opc);
  }
This article has been dead for over six months. Start a new discussion instead.