Thread: Menu (any default style ? )

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    66

    Menu (any default style ? )

    I am currently practice to upgrade my (novice) skills and I want to know if there any default/universal way of coding the menu choices. For example:

    1. Wake up
    2. Sleep
    3. Sit Down
    4. Take Home
    Select you choice:

    I thought of this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    int DoMenu()
    {
        int choice;
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
        
        cin >> choice;
        return choice;
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
        while (!fQuit)
        {
              choice = DoMenu();
              
              if ( choice < Wake || choice > Quit )
              {
                   cout << "Wrong pal";
                   cout << endl << endl;
                   
                   continue;
              }
              
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                    fQuit = true;
                                    cout << "\nExiting .... " << endl << endl;
                                    break;
              }
        }
        system("PAUSE");
        return 0;
    }
    ButI think that the above source code is strange a bit. Is there any particular style that you, experienced programmers use ?
    Last edited by BlackSlash12; 09-04-2007 at 01:46 PM. Reason: debugging

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    In the meantime I thought this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    void ShowMenu()
    {
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
    }
    
    void InputCheck(int choice)
    {
         while ( choice < Wake || choice > Quit )
         {
               cout << "/nWrong! Retry ..." << endl;
               cout << "Select you choice (in number) :";
           cin >> choice;
         }
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
              ShowMenu();
               cout << "Select you choice (in number) :";
               cin >> choice;
              InputCheck(choice);
              
                        
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                    fQuit = true;
                                    cout << "\nExiting .... " << endl << endl;
                                    break;
              }
        system("PAUSE");
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Well I think that is best:

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    int DoMenu()
    {
        int choice;
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
        
        cin >> choice;
        return choice;
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
        while (!fQuit)
        {
              char  reSelect;
              choice = DoMenu();
          
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                    fQuit = true;
                                    cout << "\nExiting .... " << endl << endl;
                                    break;
              }
              
              if (fQuit == false)
              {
                        cout << "\n\t\tDo you want to select again ? (Y/N):";
                        cin >> reSelect;
              
                        if ( reSelect = 'Y' )
                        {
                            continue;
                        }
              }
    }        
              
        system("PAUSE");
        return 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Comments in the code in red!

    Quote Originally Posted by BlackSlash12 View Post
    Code:
    ...
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
    So, if the poor user presses any other number, it automatically quits the application
                                    fQuit = true;
                                    cout << "\nExiting .... " << endl << endl;
                                    break;
              }
              
              if (fQuit == false)
              {
                        cout << "\n\t\tDo you want to select again ? (Y/N):";
                        cin >> reSelect;
            
      
                        if ( reSelect = 'Y' )  // Assigning reSelect with 'Y' will always be true, as 'Y' is not 0!
                        {
    What's te purpose of this bit then? Do you actually understand what this does?
                            continue;
                        }
    Assuming the user pressed "N", what is supposed to happen? [And assuming your assignment is changed to a test for equality]
              }
    }        
              
        system("PAUSE");
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    So...what to do ? Could you fix it or post your code-design style ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BlackSlash12 View Post
    So...what to do ? Could you fix it or post your code-design style ?
    Well, if you want the user to quit due to selecting an invalid choice, that's your choice - but I prefer applications that let me be a bit clumsy - do you ever hit the wrong key?

    As to your use of assignment instead of test for equality, I would prefer this:
    Code:
                        if ( 'Y' == reSelect )
                        {
                            continue;
                        }
    But of course, there's no reason to do any of it, because it makes no difference - continue is only useful in a loop if there's something else to do after the continue statement. In this case, there's just closing braces between this point and the end of the while-loop, so the whole if-statement is pointless.

    Of course, the other unanswered question is what you want to do if N is pressed - is that supposed to do something, or is Y and N equivalent - if so, why do you ask the user to enter Y or N? [And of course, what happens if it's neither?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I want to display the menu choices and then let the user to select one. That's ok.

    Afterwards I wan to create a input check.

    Code:
    while (choice > 5 || choice <0)
    {
          cout << "You wrong";
          cout << "Retype: ";
          cin >> choice;
    }
    Is this code block, ok ?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BlackSlash12 View Post
    I want to display the menu choices and then let the user to select one. That's ok.

    Afterwards I wan to create a input check.

    Code:
    while (choice > 5 || choice <0)
    {
          cout << "You wrong";
          cout << "Retype: ";
          cin >> choice;
    }
    Is this code block, ok ?
    Yes, except you may want to use the enums instead of numbers - otherwise, what's the point of using enums in the first place?

    edit: And by the way, the code you show here will allow zero as a valid choice, which isn't valid. Another reason to use the enum.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    You right!Fixed but what about the loop ? I want the opportunity to reselect again (if pressed y) or exit (if pressed n).

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    int DoMenu()
    {
        int choice;
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
        
        cin >> choice;
        while (choice > Quit || choice < Wake)
    {
          cout << "You wrong! ";
          cout << "Retype: ";
          cin >> choice;
    }
        return choice;
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
        while (!fQuit)
        {
              char  reSelect;
              choice = DoMenu();
          
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                    fQuit = true;
                                    cout << "\nExiting .... " << endl << endl;
                                    break;
              }
              
              cout << "Do you want to select another one ?  (y/n) :";
              cin >> reSelect;
            
            /// What now ???               
              
              
    }        
              
        system("PAUSE");
        return 0;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you think it should do to reselect?

    I don't really understand why you want to have a yes-no question there, rather than just go back to the menu - there is a menu choice to exit, right? So why have another question that asks if you want to exit or continue?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    For example if I select 3 a particular message is printed. How can I come back to the menu ? I do not know .... so I invested y/n.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I think I made it!

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    int DoMenu()
    {
        int choice;
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
        
        cout << "Please select :";
        cin >> choice;
        while (choice > Quit || choice < Wake)
    {
          cout << "You wrong! ";
          cout << "Retype: ";
          cin >> choice;
    }
        return choice;
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
        while (!fQuit)
        {
              char  reSelect;
              choice = DoMenu();
          
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                     fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
              }
              
              if (choice != Quit )
              {
                         cout << "Do you want to select another one ?  (y/n) :";
                         cin >> reSelect;
                         while (reSelect != 'y' && reSelect != 'n')
                         {
                               cout << "You wrong! Type 'y' or 'n' :";
                               cin >> reSelect;
                         }
                         if (reSelect == 'n')
                         {
                                    cout << "\nExiting .... " << endl << endl;  
                                    break;
                         }
              }
                               
            
            /// What now ???               
              
              
    }        
              
        system("PAUSE");
        return 0;
    }
    Right isn't it ? If you wish you could post your way (without y/n).

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Sorry for double post, I just found what you mean.

    Code:
    #include <iostream>
    
    using namespace std;
    
    enum CHOICE  {
         Wake = 1,
         Bath,
         Toilet,
         Hack,
         Quit   };
    
    // Function Definition and Prototype
    
    int DoMenu()
    {
        int choice;
        
        cout << endl << endl;
        cout << " ***** Menu ***** " <<endl;
        cout << "(1) Wake me up when project ends" << endl;
        cout << "(2) GOTO Bathroom" << endl;
        cout << "(3) fflussh(toilet)" << endl;
        cout << "(4) Hack the kernel" << endl;
        cout << "(5) Quit" << endl;
        
        cout << "Please select :";
        cin >> choice;
        while (choice > Quit || choice < Wake)
    {
          cout << "You wrong! ";
          cout << "Retype: ";
          cin >> choice;
    }
        return choice;
    }
    
    int main()
    {
        int choice;
        bool fQuit = false;
        
        while (!fQuit)
        {
              char  reSelect;
              choice = DoMenu();
          
              switch(choice)
              {
                            case Wake:
                                 cout << "Drinsane!!! " << endl;
                                 break;
                            case Bath:
                                 cout << "Please wait while bathing ... " << endl;
                                 cout << "You smell sehr gut" << endl;
                                 break;
                            case Toilet:
                                 break;
                            case Hack:
                                 break;
                            case Quit:
                                 fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
                            default:
                                     fQuit = true;
                                 cout << "\nExiting .... " << endl << endl;
                                 break;
              }
              
              if (choice != Quit )
              {
                         continue;
              }
                               
            
            /// What now ???               
              
              
    }        
              
        system("PAUSE");
        return 0;
    }
    Is this ?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I still don't understand the reason for your "if (choice != Quit) continue;" - it's not needed, the while-loop will continue anyways, until your end-condtion (fQuit) becomes true.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    You right! Do you prefer using enum or not ?

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. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  4. Menu stuff
    By Shadow in forum C Programming
    Replies: 10
    Last Post: 04-28-2002, 09:05 AM