Thread: Reading a character from the keyboard getchar()

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Reading a character from the keyboard getchar()

    hello all

    In a do while I'm printing a menu, getting a character from the keyboard and run a switch statement in the cases where that character is "d, v or f". the condition executes until the keystroke is 'e'.

    using the getchar() function I get my menu printed twice. What am I doing wrong?

    Code:
    char check;
    do {
           prinf("Menu:");
                   ...
           check = getchar();
           switch (check)
           {
                case 'f':
                
                case 'd':
     
                case 'v':
    
                default:
           }
         } while(check != 'e');

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Well actually It prints once and then runs the default case and prints again.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by mnml View Post
    Well actually It prints once and then runs the default case and prints again.
    This happens because the newline character is being fetched the second time. For what you want, you could use:
    scanf("%c\n", &check);
    "scanf()" performs pattern matching, so you could use it to your advantage.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    With scanf() I need to press a character, Enter, and then another character which is the option from the menu to run the switch statement.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    this is my complete do while condition


    Code:
    char menu;
    
     do {
            printf("MAIN MENU\n");
            printf("[d] Display Empty Chess Board\n");
            printf("[v] Check Validiti of Chess Notation\n");
            printf("[s] Save Moves to e Text File\n");
            printf("[e] End program\n");
            printf("Selection [d, v, s, f, e]: ");
            
            scanf("%c\n", &menu);
            
            if (menu != 'e')
            {
               switch(menu)
               {
                   case 'd':
                     display_board();
                     break;
                
                   case 'v':
                     check_move();
                     break;
                     
                   case 's':
                     save_move();
                     break;
                
                   default:
                     printf("wrong character. Print menu again\n\n");
                     break;
               } // switch
            } // if
         } while(menu != 'e');
    Now with scanf() thats what I get in the console the first time it executes:

    MAIN MENU
    [d] Display Empty Chess Board
    [v] Check Validiti of Chess Notation
    [s] Save Moves to e Text File
    [e] End program
    Selection [d, v, s, f, e]: d
    d

    Notice that I type "d - Enter - d" before the switch statement executes.

    then display_board() runs and the menu is printed again. Not only that but when I give 'v' after that, display_board() executes again and not the check_move() funtion
    Last edited by mnml; 10-21-2011 at 08:23 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try it this way...
    Code:
    scanf (" %c", &menu);
    Note the space between the first quote and the % sign... that tells it to ignore invisible characters.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-15-2009, 10:31 AM
  2. reading a string from keyboard
    By Mona777 in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2009, 11:30 AM
  3. How do I input an unbuffered character via the keyboard???
    By Michael_in_Ohio in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 12:00 PM
  4. Replies: 2
    Last Post: 01-10-2002, 07:42 PM
  5. reading the keyboard
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 04:11 PM