Thread: A menu using Characters

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    A menu using Characters

    Hello! I was wondering how I would be able to create a menu to select Characters instead of numbers.

    Currently this is what i have:

    Code:
    int menu(){
        int choice;
        cls;
           printf("\n\n==============================\n");
           printf("   MY FIRST ARRAY ASSIGNMENT ");
           printf("\n==============================\n");
           printf("L - oad the Array\n");
           printf("M - inimum value from the Array\n");
           printf("H - ighest value from the Array\n");
           printf("A - verage of all values in the Array\n");
           printf("O - utput the values in the Array\n");
           printf("Q - uit\n");
           printf("==============================\n");
           printf("\t Enter Menu Choices: ");
           scanf("%i", &choice);
           
           switch(choice){
                          case 1:                          
                          loadArray(grades);
                               break;                           
                          case 2:
                               break;                           
                          case 3:                          
                               break;
                          case 4:      
                               break;
                          case 5: 
                               break;
                          case 6:
                               break;
                          default:
                                  printf("You must enter a L,M,H,A,O,Q only!\n");
                                  pause;
                               break;
           } // End of switch
           } // End of Function

    I want each case to select from: L M H A O Q, instead of 1-6


    thank you!

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    One way to do it (if you need to use the switch statement) would be to save your choice to a char type variable, then assign an int type variable the value of the char type variable (e.g. intvar=charvar). Then run your switch statement with the value of the int variable.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    I don't need to use the switch statement, that is currently the only way I know to select from a menu. I'm going to research the char type variable though, thank you!

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    I was actually abbreviating for character type. That's the actual name of the variable type.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when you successfully change your input routine to work with chars your switch will look like:
    Code:
    switch(choice){
          case 'L':                         
                          loadArray(grades);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cls;
    If this is some macro to change the screen, then change the macro.

    A function call should LOOK like a function call to the casual observer. That is, you need to write
    cls();
    in the source code, and fix the macro so it can be invoked as such.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu
    By Drake in forum Game Programming
    Replies: 12
    Last Post: 11-29-2005, 12:02 PM
  2. menu help
    By joe44 in forum C Programming
    Replies: 4
    Last Post: 05-05-2004, 10:04 AM
  3. Menu Help
    By CJ7Mudrover in forum C Programming
    Replies: 7
    Last Post: 03-09-2004, 09:59 AM
  4. need help with menu
    By TopJo in forum C Programming
    Replies: 2
    Last Post: 06-23-2003, 08:33 PM
  5. How to modify a menu into a menu Folder(contains submenus) ??
    By L.O.K. in forum Windows Programming
    Replies: 3
    Last Post: 01-09-2003, 02:26 PM