Thread: main menu

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

    main menu

    Hey guys just wondering how would i go about programming main menu which allows not only integers 1 - 9 but also characters a -e

    eg

    1 add product
    2 delete product
    3 search product
    a replenish product
    b count produts
    c save and exits

    i cant change the source code i currently am using "if else" statements to enter the integer values in the menu.

    But im not sure becuase we use fgets and then it uses ascii to integer to make the conversion to an int

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could read both choice as a char and just use the ASCII value of the char in a switch statement.

    Since you said you can't change the source code, I'm not sure what you really mean, since you'd have to make some changes to do this.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    we use fgets and then it uses ascii to integer to make the conversion to an int
    It starts out doing the right thing by reading a string, but then you're forced to use ints because the string is converted to an int. If you can't change the source, you can't change how it works either.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Hi guys i did this below but how come it wont recognize the 'a' character in the switch im using the decimal equivalent for it

    Code:
    int a =97;
    
    if(inputNumber == a)
    {
    
    }

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Just use the char value of 'a' right away:
    Code:
    if (inputNumber == 'a')
    {
    //...
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bazzano View Post
    Hi guys i did this below but how come it wont recognize the 'a' character in the switch im using the decimal equivalent for it

    Code:
    int a =97;
    
    if(inputNumber == a)
    {
    
    }
    Mate, show us the switch statement that you're talking about, or show us the whole code for your inputNumber == 1 code, above.

    We can't really talk smartly about code we have never seen! Without seeing the code, we can't sort your problem out.

    Adak

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    i just tried that but no code ran below it, could it be the atoi stuffing it jup


    Code:
    do
       {
          /* Print out menu */
          printf("\nMain Menu:\n");
          printf(" (a) Purchase a Drink\n");
          printf(" (b) Report low stock items\n");
          printf(" (c) Replenish stock for a given item\n");
          printf(" (d) Report sales\n");
          printf(" (e) Sales log on/off\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 (a-e,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(inputNumber == 'a')
             printf("!!!!!worked\n");
    
             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;
             }
    
             /* If user does not enter a valid input, ask user to re-enter */
             /*else
                printf("Please enter a valid option!\n");
          }*/
       }
       while(inputNumber != ABORT);

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Use a switch instead of all the if's,

    also
    Code:
    fgets(input, MAX_OPTION_INPUT + EXTRA_SPACES, stdin);
    I hope that sizeof(input) >= MAX_OPTION_INPUT + EXTRA_SPACES
    rather, just write
    Code:
    fgets(input, sizeof(input), stdin);
    Code:
             inputNumber = atoi(input);
    
             /* Process the user's request */
             if(inputNumber == 'a')
             printf("!!!!!worked\n");
    Ah!? You've just converted inputNumber to an integer, what's to say 'a' = atoi("a"); ?
    Last edited by zacs7; 04-24-2007 at 05:31 AM.

  9. #9
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    but i just changed it then to this below and it compiled cleanly but however it still didnt recognize the 'a' character when being typed in.

    Code:
    if(inputNumber == 'a')
    printf("test")
    they also dont want us to use a switch because they give us start up code that we are not aloud to adjust. Which is really annoying

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I think you mean,

    Code:
    if(input[0] == 'a')
        printf("test");
    , I'm also in Melb

  11. #11
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by bazzano View Post
    i just tried that but no code ran below it, could it be the atoi stuffing it jup


    Code:
    do
       {
          /* Print out menu */
          printf("\nMain Menu:\n");
          printf(" (a) Purchase a Drink\n");
          printf(" (b) Report low stock items\n");
          printf(" (c) Replenish stock for a given item\n");
          printf(" (d) Report sales\n");
          printf(" (e) Sales log on/off\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 (a-e,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(inputNumber == 'a')
             printf("!!!!!worked\n");
    
             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;
             }
    
             /* If user does not enter a valid input, ask user to re-enter */
             /*else
                printf("Please enter a valid option!\n");
          }*/
       }
       while(inputNumber != ABORT);
    sorry for interfering but why all this
    why didn't you just use switch
    sorry again

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    they also dont want us to use a switch because they give us start up code that we are not aloud to adjust. Which is really annoying
    Speaks for it's self

  13. #13
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Is there anyway i can make it work without a switch and with the if else statements?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Of course. The logic is the same, just that the implementation would be perhaps a little more cumbersome.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by bazzano View Post
    Is there anyway i can make it work without a switch and with the if else statements?

    Something like:
    Code:
    int choice, g;
    do {
         while ((g = getchar()) != '\n' && g != EOF); // make sure we flush the stream before we try and use it (get rid of any unwanted chars
        choice = getchar();
        if(choice == 'a')
              ; // a
        else if(choice == '1')
              ; // 1
        else
              ; // invalid input
    } while (choice != EOF);
    Last edited by zacs7; 04-24-2007 at 06:24 AM.

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