Thread: Question

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Question

    I have a menu program but want to change it from using numbers to using Characters. I think I understand most of what I need to change. But and confused on the error checking and exactly what I would be passing. OK at this point am I even being coherent in my question, and am I on the right track or should I look at doing this another way?

    Here are some samples of the code to see if that helps clear it up.
    Menu Code:
    Code:
    char getMenu (void)
    {
    	char choice;
    
    	do
    	{
    		printf("\t*             Menu               *\n");
    		printf("\t*                                *\n");
    		printf("\t* A. Function A                  *\n");
    		printf("\t* E. Function E                  *\n");
    		printf("\t* W. Last Function               *\n");
    		printf("\t* E. Get me out of here          *\n");
    		printf("\t*                                *\n");
    
    		printf("Please select a menu option and press <retrun>: ");
    		scanf("%c", &choice);
    
    // error handling is done here
    		if (choice <1 || choice >4)
    		{
    		system ("cls");
    		printf("\aSorry, but this is not a valid option. Please select again.\n\n");
    		while ( getchar() != '\n' ); 
    
     		}
    	}
    	while (choice <1 || choice >4);
    
    return choice;
    }
    As I said, I think I am on track here (but that could be wrong, I am just confused on the error hadling, and perhaps what I really should be passing. My original error handling was done for a menu program that used numbers and not letters.

    Also if I using a switch in the main to call the functions from what is passed by the menu, how do I ID the cases to get accesed? Or should I not be using a switch. As far as I know, a switch can only be numbers...Is this right or wrong? And do I convert the text to numbers for the switch or as I have said, am I totally off track and should be looking for another way to make this work?

    As always any advice and help is greatly appreciated.

    Thanks
    DD
    "aut vincere aut mori"

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    If you're doing a menu, a better way to deal with users not entering a listed option is to use switch().

    This way any invalid user input is dealt with by the default at the end of the case statements.

    In my humble opinion anyway...
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    scanf("%c", &choice);
    switch ( toupper(choice) ) {
      case 'A':
        // do a function
        break;
      case 'E':
        // do a function
        break;
      case 'W':
        // do a function
        break;
      default:
        // do error message
        break;
    }

  4. #4
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Talking Thanks

    Thanks guys that got me on the right track for now at least And I think I see some light up ahead, I just hope it is not an oncomming train

    As always I appreciate the help, guidance and advice.

    DD
    "aut vincere aut mori"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM