Thread: Remove the need for an ENTER key to be pressed.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Remove the need for an ENTER key to be pressed.

    The following 2 codes are almost identical, only that the switch statements are slightly different.

    The 2nd code has the issue of requiring an additional enter key to be pressed when I enter '3' as input to exit the program.

    Any opinions are appreciated =)

    Working code :


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void clearKeyboardBuffer() {
        int ch;
        while ((ch = getchar() != '\n') && (ch != EOF));
    }
    
    void rgr1(){
        
        char chArr[BUFSIZ];
        char choice; 
        int choiceint, rating, len;
    
        do{ 
    
            printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");
            printf("Choose a place by entering its numerical value: ");
    
            fgets(chArr,sizeof(chArr),stdin);
            sscanf(chArr, " %c", &choice); 
            
            len = strlen(chArr);
            
            // converts scanned char to int 
            choiceint = choice - '0';
    
            if(!isdigit(choice) || len > 2 || choiceint > 3){ 
                    printf("You did not enter an accepted digit.\n");
            }   
            else if(choiceint != 3){ 
                    switch(choiceint){
                            case 1:
                                    printf("You chose the Zoo.\n");
                                    printf("your rating : ");
                                    scanf("%d", &rating);
                                    break;
                            case 2:
                                    printf("You chose the Mall.\n");
                                    printf("your rating : ");
                                    scanf("%d", &rating);
                                    break;
                    }// end of switch
    
            clearKeyboardBuffer();
    
            }// end of else if
            else{
    
            }// end of else
    
    }// end of do 
    while(choiceint !=3 || len > 2);
    }
    Shortened code, but an additional enter key is required to be pressed when I enter '3' to exit the application.

    Code:
    #include <stdio.h>
    #include<ctype.h>
    #include <string.h>
    
    void clearKeyboardBuffer() {
        int ch;
        while ((ch = getchar() != '\n') && (ch != EOF));
    }
    
    void rgr2(){
        
        char chArr[BUFSIZ];
        char choice;
        int choiceint, rating, len;
        char *p;
        
        do{
            
        printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");
        printf("Choose a place by entering its numerical value: ");
        fgets(chArr,sizeof(chArr),stdin);
        sscanf(chArr, " %c", &choice); 
        
        len = strlen(chArr);
    
        choiceint = choice - '0';
    
            if(!isdigit(choice) || len > 2 || choiceint > 3 ){
                printf("You did not enter an accepted digit.\n");
            }
            else{         
                switch(choiceint){
                        case 1:
                            printf("You chose the Zoo.\n");
                            printf("your rating : ");
                            scanf("%d", &rating);
                            break;
                        case 2:
                            printf("You chose the Mall.\n");
                            printf("your rating : ");
                            scanf("%d", &rating);
                            break;     
                        default:
                            break;
                }// end of switch
                
                clearKeyboardBuffer();
                
            }// end of else 
    
            }// end of do 
        while(choiceint !=3 || len > 2);
        
    }

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    clearKeyboardBuffer is called unconditionally, but in the case that choiceint is 3, there are no remaining characters in the stream. Therefore getchar will block for more input.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to unify your input method? always use the fgets+sscanf pair and you will eliminate the need of clearKeyboardBuffer completely (or use it only in the case when the buffer filled by the fgets is full and does not contains \n - meaning user entered more chars than the buffer could hold)
    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

  4. #4
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Quote Originally Posted by sonjared View Post
    clearKeyboardBuffer is called unconditionally, but in the case that choiceint is 3, there are no remaining characters in the stream. Therefore getchar will block for more input.
    I am sorry, I don't get the part of "no remaining characters in the stream" =/. Could you explain further ?

    Quote Originally Posted by vart View Post
    why not to unify your input method? always use the fgets+sscanf pair and you will eliminate the need of clearKeyboardBuffer completely (or use it only in the case when the buffer filled by the fgets is full and does not contains \n - meaning user entered more chars than the buffer could hold)
    Do you mean unifying the input method as in joining the fgets and sscanf in a single statement instead of separated statements ?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Krabiki View Post
    I am sorry, I don't get the part of "no remaining characters in the stream" =/. Could you explain further ?
    It means that when user chooses choice 3 the input stream (stdin) contains 2 characters '3' and '\n'
    Both these characters are read from the stream by fgets leaving it "empty"
    Any function trying to read from such stream will pause till the next input is made

    So calling at this time the clearKeyboardBuffer will pause the program execution till the \n is entered or EOF condition is emulated by pressing Ctrl+Z



    Quote Originally Posted by Krabiki View Post
    Do you mean unifying the input method as in joining the fgets and sscanf in a single statement instead of separated statements ?
    No I mean to get rid of scanf calls and call fgets and sscanf instead
    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
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Smile

    Quote Originally Posted by vart View Post
    It means that when user chooses choice 3 the input stream (stdin) contains 2 characters '3' and '\n'
    Both these characters are read from the stream by fgets leaving it "empty"
    Any function trying to read from such stream will pause till the next input is made

    So calling at this time the clearKeyboardBuffer will pause the program execution till the \n is entered or EOF condition is emulated by pressing Ctrl+Z
    Hmm isn't it the same case when the user enters '1' or '2' ? There shouldn't be any characters in the stream too right ?
    Or is it because for '1' and '2' , the presence of scanf causes some additional characters to be present ?


    Quote Originally Posted by vart View Post
    No I mean to get rid of scanf calls and call fgets and sscanf instead
    Wow after changing all scanf calls to fgets and sscanf, the whole issue is solved. I didn't know scanf is really such a danger to use.. Is this because scanf doesn't remove extra characters from the buffer ?

    Thanks a lot

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by Krabiki View Post
    I am sorry, I don't get the part of "no remaining characters in the stream" =/. Could you explain further ?
    The whole point of the clearKeyboardbuffer function is to remove characters remaining in the stream. Specifically that would be a trailing '\n' left over by mixing formatted input (eg. scanf) and unformatted input (eg. getchar or fgets). The combination of fgets and sscanf takes care of itself because fgets will extract the '\n' from the stream and therefore clearKeyboardBuffer is not needed. However, in the non-default cases of the switch scanf is used directly, where clearKeyboardBuffer is needed.

    You call clearKeyboardBuffer in all cases, so it should be no surprise that one of those paths would not behave the way you want.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Very clear explanation sonjared , many thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to take input till enter is pressed
    By shruthi in forum C Programming
    Replies: 10
    Last Post: 08-25-2012, 12:34 PM
  2. How could I make my program switch Sleep() while enter is pressed?
    By cplusplusnoob in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2012, 05:32 PM
  3. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  4. How to find if enter was pressed in an edit control?
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2007, 07:25 PM
  5. how do you know when enter is pressed?
    By pinkcheese in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2002, 12:45 PM