Thread: need help with menu

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    need help with menu

    This is the menu code that I'm using now. I would like this to be like a dos prompt. I want to type the command "dir" and it will go to that function. Any help would be appreciated.
    Code:
    do { 	
             printf("\nPlease select an option (1-4):\n");
             printf("1) Save\n");
             printf("2) Del\n");
             printf("3) Dir\n");
             printf("4) Quit\n");
          
             scanf("%d",&choice);
          
             switch (choice) {
                case 1:
                   Save();
                   break;
                case 2:
                   Del();
                   break;
                case 3:
                   Dir();					
                   break;
                case 4:
                   break;
                default:
                   printf("Invalid choice");
                   break;
             }
          } while(choice != 4);

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    If you want to work with strings instead of single characters it will be trickier if you have never done it. Take a look at strcmp() and fgets(), if you can figure out how to use those two you can make the user press anything and get all kinds of commands

    Here is a quick example of how fgets() and strcmp() works.
    Code:
    int array[100];
    
    fgets(array, sizeof(array), stdin);
    
    if (strcmp(array, "dir") == 0) {
            // do something
    }
    fgets() takes three arguements, the first is where you will hold the users info, the second is the size in bytes of the array, and the third is a pointer to the stream you are reading. In this case, since its the terminal, I used stdin cause it points to that usually.
    strcmp() just compares the first and second arguements and returns 0 if successful. This is very generic, and has no error checking etc, check out the FAQ for a more detailed and better way to use fgets(). I know it's alot to grasp at once, especially if you have never worked with arrays and stuff, goodluck

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a thread on this below a ways down. Read the command into a buffer, use strcmp to compare arguments. You may want to run your argument through something to smash the case so that "Dir" and "DIR" and "dir" are all considered the same thing.

    [edit] Curses! Foiled again! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM