Search code examples
cswitch-statementreturn-valuemenuitem

How Can I Pass a Menu Function Int Value Back To Main To Access Other Menu Functions Using Switch Statement?


I'm doing this for part of a school project and I am so lost and this is only the beginning of it. Our professor wants us to have 4 menu functions. Each menu has options to access other functions within the program. First, we are asked to have the program state if we would like to start or quit. That's no problem. My problem is when I run the main menu function and select an option I cannot get my choice to return to main to run the switch case to access the other menus. Right now I have all the other menus saying "coming soon..." just so I know I am getting it right. I'll add more once I get past this part. This is my first post here so I apologize if this is a lot of code to post. I greatly appreciate any help. Thank you.

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


int main()
{
    //declare all working variables: mOption, FManOption, COption...etc...
    int MOption = 0;
    int FManOption = 0;
    int FOption = 0;
    int COption = 0;
    int userChoice = 0;


    int n = mainMenu();

        switch(n)
        {
            case 1: while(FManOption != 3)
                    {
                        FManOption = FishermanMenu();
                        switch(FManOption)
                        {
                            case 1: //get a fisherman
                                    //count fisherman
                                    break;
                            case 2: //prompt for a ssn, validate, search
                                    //if found display everything about this fisherman
                                    break;
                            case 3: //hit any key to go back to main menu
                                    //reset FManOption
                                    break;
                            default: "error!";
                        }//end switch(FManOption)
                    }//end while(FManOption != 3)
                    break;
            default: printf("error");
        }

    return 0;
}
int mainMenu()
{
  int option = 0;

    printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
    do
    {
        printf("1 - Fisherman menu\n");
        printf("2 - Fish menu\n");
        printf("3 - Tournament(Catch) menu\n");
        printf("4 - Close Tournament (determine winner)\n");
        printf("5 - Quit Program\n\n");
        printf("Please select a menu option: ");
        if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
        {
          /* handle input error */
          return -1;
        }
    } while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */

    return option; /* finally return the final correct option */
}

int FishermanMenu()
{
    printf("Coming soon...");
    /*
    -1-Register fisherman

    -2-Search fisherman

    -3-Go back to main menu
    */
    //FManOption
}//end Fisherman Menu

Solution

  • Your mainMenu() function doesn't return anything other than 0, in addition you ignore the return value in main, when you call mainMenu(); (this shouldn't even compile btw), this is probably what you are looking for,

    int mainMenu(void)
    {
      int option = 0;
    
        printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
        do 
        {
            printf("1 - Fisherman menu\n");
            printf("2 - Fish menu\n");
            printf("3 - Tournament(Catch) menu\n");
            printf("4 - Close Tournament (determine winner)\n");
            printf("5 - Quit Program\n\n");
            printf("Please select a menu option: ");
            if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
            {
              /* handle input error */
              return -1;
            }
        } while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */
    
        return option; /* finally return the final correct option */
    }
    
    int main(void)
    {
    
      int n = mainMenu(); /* save the result to n */
    
      /* your code */
    
      return 0;
    }