Thread: main menu

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also remember that ASCII '9' is different from the decimal constant 9...

  2. #17
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Is there a way of doing it with fgets?

  3. #18
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes

    Code:
    char buf[8]; // we only really need 1 + NUL (but better safe than sorry!)
    fgets(buf, sizeof(buf), stdin);
    if(buf[0] == 'a')
        ; // a!
    else if(buf[0] == '1')
        ; // 1
    else
        ; // invalid input
    'N yeah you can put it in a loop if you want...

  4. #19
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    int main()
    {
        int ch;
        
        printf(" ( a ). Aplha \n");
        printf(" ( b ). Beta \n");
        printf(" ( 1 ). One \n");
        printf(" ( 2 ). Two \n");
        
        printf("Enter choice\n?");
        ch = getchar();
        
        while( ch != 'q' )
        {
               switch( ch )
               {
                    case 'a':
                         printf("You entered A\n");
                         break;
                    case 'b':
                         printf("You entered B\n");
                         break;
                    case '1':
                         printf("You entered 1\n");
                         break;
                    case '2':
                         printf("You entered 2\n");
                         break;
               }
               while((ch=getchar()) != '\n' && ch != EOF);    
               printf("Enter choice\n?");
               ch = getchar();
        }
        
        while((ch=getchar()) != '\n' && ch != EOF);    
        getchar();
        return 0;
    }
    you could even just use getchar function to read a char from the user. That's the actual choice from the user.

    ssharish2005

  5. #20
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    main menu

    Hey guys iv done if(input == 97) which repesents 'a' in ascii that works if i do it once but however it does not work for ascii 98 which is 'b' and so on. I can only use this if else statements i cant use switches. And i have to use fgets

    Code:
     do
       {
          /* Print out menu */
          printf("\nMain Menu:\n");
          printf(" (1) Hot Drinks Summary\n");
          printf(" (2) Cold Drinks Summary\n");
          printf(" (3) Detailed Menu Report\n");
          printf(" (4) Add Menu Category\n");
          printf(" (5) Delete Menu Category\n");
          printf(" (6) Add Menu Item\n");
          printf(" (7) Delete Menu Item\n");
          printf(" (8) Save & Exit\n");
          printf(" (9) Abort\n");
          printf("\nSelect your option (1-9): ");
    
          /* Store user's input and check for correct length */
          fgets(input, MAX_OPTION_INPUT + EXTRA_SPACES, stdin);
    
       /* Convert the user's input into an integer */
          inputNumber = atoi(input);
    
          /* Process the user's request */
         if(input[0] == 97) 
         printf("character a \n");
    
         else if(input[1] == 98)
         printf("dude");
    
         else if(inputNumber == 1)
             displaySummary(&menu, HOT);
    
          else if(inputNumber == 2)
             displaySummary(&menu, COLD);
    
          else if(inputNumber == 3)
             categoryReport(&menu);
    
          else if(inputNumber == 4)
             addCategory(&menu);
    
          else if(inputNumber == 5)
             deleteCategory(&menu);
    
          else if(inputNumber == 6)
             addItem(&menu);
    
          else if(inputNumber == 7)
             deleteItem(&menu);
    
          else if(inputNumber == 8)
          {
             saveData(&menu, menuFile, submenuFile);
             printf("Changes saved successfully! Now exiting...\n");
             systemFree(&menu);
             return EXIT_SUCCESS;
          }
    
          else if(inputNumber == 9)
          {
             /* If user abort's program, free all allocated memory before
                exiting */
             printf("Thank you for using this program. Now exiting...\n");
             systemFree(&menu);
             return EXIT_SUCCESS;
          }
    
       }
       while(inputNumber != ABORT);
    
       return EXIT_SUCCESS;
    }

  6. #21
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    else if(input[1] == 98)
    Why are you testing the second char when testing for 'b' ?
    what's wrong with making it more portable ( and readable ) like this ?
    Code:
    else if(input[0] == 'b')
    Kurt

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Said the night wind to the little lamb,
    Code:
    if(input[0] == 97) 
         printf("character a \n");
    
         else if(input[1] == 98)
    "Do you see what I see?"


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

  8. #23
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Wouldn't it be wonderful if we could replace 97 or 98 by something more meaningful like ... 'a' and 'b' ?

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Can you do that?!


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

  10. #25
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    sorry guys just did that for testing purposes because i couldnt get it working ill try. I may of been pointing to the wrong element in the array

  11. #26
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by bazzano View Post
    I may of been pointing to the wrong element in the array
    that hypothetic formulation is not necessary in this context

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Blur on.. skipping codings/go to.
    By cheongzewei in forum C Programming
    Replies: 4
    Last Post: 11-14-2008, 06:07 AM
  3. How to put a tick in a windows menu?
    By cloudy in forum Windows Programming
    Replies: 1
    Last Post: 07-08-2007, 05:02 PM
  4. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  5. Systray menu messages
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2005, 06:25 AM