Thread: Creating a Menu in C

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    Creating a Menu in C

    Hi, and thanks in advance for any help you can give me.

    I have written 6 small programs and am trying to create a menu that allows the user to view whichever one they wish by calling from that file. I have written the code below for a menu but I don't know how to actually add in the programs themselves. Do they need to be in the same folder? Can the file names have spaces? Do I declare them somewhere at the beginning and then have the relevant one in the case statements?

    Any help would be greatly appreciated.


    Code:
    #include <stdio.h>
    
    /*Main Menu*/
    
    
    int main ()
    {
        int choice;
    
    
        printf("\n \t\t Programs \n");
        printf("----------------------------------------------- \n");
        printf("\t 1. Example Program 1 \n");
        printf("\t 2. Example Program 2 \n");
        printf("\t 3. Example Program 3 \n");
        printf("\t 4. Example Program 4 \n");
        printf("\t 5. Example Program 5 \n");
        printf("\t 6. Example Program 6 \n");
        printf("\t 0. Exit \n\n");
        
    
    
        printf("Please choose which program you would like:  \n");
        scanf("%d", &choice);
        printf("Choice = %d \n", choice);
        
        switch (choice)
        {
            case 1 : printf("You have chosen program %d:Example Program 1 \n", choice) ; break;
            case 2 : printf("You have chosen program %d:Example Program 2 \n", choice) ; break;
            case 3 : printf("You have chosen program %d:Example Program 3 \n", choice) ; break;
            case 4 : printf("You have chosen program %d:Example Program 4 \n", choice) ; break;
            case 5 : printf("You have chosen program %d:Example Program 5 \n", choice) ; break;
            case 6 : printf("You have chosen program %d:Example Program 6 \n", choice) ; break;
            case 0 : printf("You have chosen %d to exit \n", choice) ; return ; break;
    
    
            default : printf("This is not a valid choice. Please choose again"); break;
        } 
        
        
        return 0;
    }
    Thanks again.

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    my suggestion is to just copy and paste each program under each case. Sorry if I didn't help haha

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You may just need something like this, using system().
    Spaces in filenames can be dealt with using quotes.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int n = -1;
        while (n != 0) {
            printf("1) one\n2) two\n0) quit\n>>> ");
            scanf("%d", &n);
            switch (n) {
            case 0: break;
            case 1: system("'./one o rama'"); break;
            case 2: system("./two"); break;
            default: printf("unknown option\n");
            }
        }
        return 0;
    }
    I haven't tested on windows. It may need to be double quotes:
    Code:
            case 1: system("\"./one o rama\""); break;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating A Menu
    By Devon Smith in forum C Programming
    Replies: 3
    Last Post: 03-31-2013, 02:37 AM
  2. Creating a menu C
    By DeanWinchester in forum C Programming
    Replies: 2
    Last Post: 01-05-2012, 04:07 PM
  3. Creating a C menu using Up Down
    By RockLee in forum C Programming
    Replies: 7
    Last Post: 03-15-2011, 06:02 PM
  4. creating menu
    By spikestar in forum C Programming
    Replies: 3
    Last Post: 09-19-2009, 11:16 PM
  5. creating a menu using C
    By whitey82 in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 12:43 PM

Tags for this Thread