Thread: Menu Options Verification?

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Menu Options Verification?

    Is there an easier way to check the menu options without using the "option != "a" && "b" && ...."c".? Can I just check if character in string and simplify the if and while loops?

    Code:
    char getMenuOption ()   {
    	
    	char option;
    	option = NULL;  // Initialize variable to null
    	
    	do { 
    		clrScr ();
    
    		cout << endl;
    		cout << " *********************************************************\n";
            cout << " *\t               B A G   M E N U        \t\t *\n";
    		cout << " *********************************************************\n";
    	    cout << " *\t\t\t\t\t\t\t *\n";
    	    cout << " *   [P]ut an item in the bag\t\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   [G]rab an unknown item from the bag\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   [D]ump the bag\t\t\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   [F]ind an item in the bag\t\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   Ge[T] a specific item from the bag\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   [W]hat's in the bag (verify)\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *   [Q]uit\t\t\t\t\t\t *\n";
    		cout << " *\t\t\t\t\t\t\t *\n";
    		cout << " *********************************************************\n";
    	
    		//  check for null so that error does not appear before input 
    		if (option != NULL) {  
    			if (option != 'P' && option != 'G' && option != 'D'
    				&& option != 'F' && option != 'T' && option != 'W'
    				&& option != 'Q')
    
    				cout << "\a\n *** ATTENTION ***  Use Selections:";
    				cout << " P, G, D, F, T, W, or Q!\n";
    
    		} // end if option not null
    
    		cout << "\n Enter: (P, G, D, F, T, W, or Q) => ";
    		cin >> option;
    
    		option = toupper (option); // Change to upper case 
    
    	} // end do while option
    	while (option != 'P' && option != 'G' && option != 'D'
    			&& option != 'F' && option != 'T' && option != 'W'
    			&& option != 'Q');
    
    	return option;
    
    }  // end getMenuOption

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Code:
    			if (option != 'P' && option != 'G' && option != 'D'
    				&& option != 'F' && option != 'T' && option != 'W'
    				&& option != 'Q')
    
    				cout << "\a\n *** ATTENTION ***  Use Selections:";
    				cout << " P, G, D, F, T, W, or Q!\n";
    this is not going to work as expected, you need curly brackets because you have more than one statement after the if

    -d-
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    The code works okay, but I wanted to find a way to simplify it for readability and ease of use.

    I just typed the code as pseudocode but is there something that will do the following type of functionality?

    Code:
    If (option == ("WTGXRQ"))
       Success = true;  // if keypress is in the set: WTGXRQ

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    Here is a pseudocode idea of what you might want to do


    Code:
    int main()
    {
    
         do
         {
    
              char answer = toupper(getMenuOption());
    
                   switch(answer)
                   {
    
                             case 'P':  function_call();
                                            break;
    
                             case 'G':  function_call();
                                            break;
    
                             case 'D':  function_call();
                                            break;
    
                             case 'F':  function_call();
                                            break;
    
                             case 'T':  function_call();
                                            break;
    
                             case 'W':  function_call();
                                             break;
    
                             case 'Q':  function_call();
                                             break;
    
     
                             default:  cout << "\n\nInvalid Option, try again.";
    
         }while (answer != 'Q');
    
    return 0;
    
    }

    switch/case structure is not only easy to use.. but is probably the best way to handle menu options.
    Last edited by The Brain; 09-13-2004 at 05:27 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Maybe something like this... not very pretty either
    Code:
    switch(int(option)){
      case: 80 // P
      case: 71 // G
      case: 68 // D
      case: 70 // F
      case: 84 // T
      case: 87 // W
      case: 81 // Q
        break; // for the above cases
      
      default:
        cout << "\a\n *** ATTENTION ***  Use Selections:";
        cout << " P, G, D, F, T, W, or Q!\n";
    
    }
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >case: 80 // P <etc...>
    Why not use character literals? That way the code is more readable, more portable, and you can avoid the comment. And your label syntax is wrong:
    Code:
    switch(option){
      case 'P':
      case 'G':
      case 'D':
      // etc...
        break; // for the above cases
      
      default:
        cout << "\a\n *** ATTENTION ***  Use Selections:";
        cout << " P, G, D, F, T, W, or Q!\n";
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Yikes! Thanks Prelude, I must return to active coding.... Seeing lots of php recently hasn't helped either.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Lost BIOS menu options?
    By PJYelton in forum Tech Board
    Replies: 3
    Last Post: 11-14-2004, 08:23 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Disabling the main frame menu options
    By MPC_Engr in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2003, 09:04 AM