A Simple Oversight

This is a discussion on A Simple Oversight within the C Programming forums, part of the General Programming Boards category; Hey Folks, This recursive menu function is supposed to only list the options once, before menu_shown is incremented. However, the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Cool A Simple Oversight

    Hey Folks,

    This recursive menu function is supposed to only list the options once, before menu_shown is incremented. However, the options are still being listed each time I default. Does anyone see why this is happening? Also, should I use fpurge instead of fflush? Thanks in advance for any insights. menu_shown is initialized globally as
    Code:
     int menu_shown=0;
    Code:
    menu ()                                 /* gets menu choice and calls */         
    {                                       /* the appropriate function   */
         char choice=0;
         int exit_code;
         
         if (menu_shown == 0);
         {     
            printf ("\nYou have the following options:\n");
            printf ("1) Add new contact\n");
            printf ("2) Search\n");
            printf ("3) Quit\n");
            menu_shown++;
         }
         printf ("\nPlease choose an option.\n>");
         choice = getchar();
         fflush(stdin);
    
         switch (choice)
         {
                case '1' : 
                     break;
                
                case '2' :
                     search ();
                     
                case '3' :
                     exit (exit_code);
                     
                default   :                                    
                     printf ("You've only got three choices, how hard can it be?\a\n");
                     menu ();
         }
    }

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Must say it's a weird thing to write a menu as a recursive fonction.
    But it's ok.

    I don't see why it would print your menu more than once. Might be missing something. Still, the condition looks okay, it's not an affectation operator but a comparaison one so it should be fine...

    And people will tell you, fflush(stdin) isn't portable, since it's not defined in C standard. Use some kind of purging function instead (something like "while (getchar() != '\n')", but you have to be careful how you use it if you don't want your users to be mad confused heh).

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Thanks for the reply. This is really stumping me. I'm using Dev C++ 4.9.9.2.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    lol. I indeed found it. This is the worst kind of error you can do, extremely hard to find out.

    Code:
    if (menu_shown == 0);
         {     
            printf ("\nYou have the following options:\n");
            printf ("1) Add new contact\n");
            printf ("2) Search\n");
            printf ("3) Quit\n");
            menu_shown++;
         }
    See that nasty ';'. Shouldn't be there. Remove me this.

    And don't do that ever again.

    Edit: by the way, if your menu function return nothing, declare your function as "void menu()".

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Rock. Thanks foxman. That semicolon has been a thorn in my side long enough.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    And perhaps this
    Code:
    fflush(stdin);
    needs to be elimiated from your code. See FAQ

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21