Thread: understanding getchar(), buffered user input, and so on...

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    understanding getchar(), buffered user input, and so on... (SOLVED)

    Sometimes I have to use getchar() once, sometimes twice. Googling I learned that sometimes we have unwanted data in the buffer.

    Cprogramming.com FAQ > Flush the input buffer says that "If you are sure that unwanted data is in the input stream, you can use some of the following code snippets to remove them." How can I be sure?

    I'm posting two links with two little programs. Both variables are of the type char, and the context where I read them with getchar() seems the same for me. Still, in one of them I use getchar() only once, and in the other I have to use it twice, and I cannot understand why.


    Well, here are the links, and there is a comment at the beginning of each of them saying where in the file I used getchar() and if I used it once or twice.

    I hope someone can clarify this to me. Thanks in advance.

    C code by fernandobasso - 68 lines - codepad

    C code by fernandobasso - 84 lines - codepad

    08_i.c

    17_peso_ideal.c
    Last edited by FernandoBasso; 10-05-2011 at 03:38 PM. Reason: Solved!!!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First... post your code here. Don't make us go chasing it... 'cause we won't.

    Buffered input means that every keystroke... even ones that don't get processed, are stored in an input buffer. At some point you need to clear it out.

    For example, when you have..
    Code:
    int c;
    printf("Enter a letter : ");
    c = getchar();
    so the prompt comes up and you press in A then you hit the <enter> key... BOTH keystrokes go into the buffer, the A is read back by the getchar() leaving the <enter> key behind... Now you need to clear that out before calling your next input function, or it will simply see you pressing <enter>.

    Worse still you could enter "My name is fred and I like to eat spam" and hit enter... The only thing that is assigned is the M, leaving the rest behind.

    The usual method is to loop until you find the newline...
    Code:
    while(getchar() != '\n');
    ... is usually adequate to ensure an empty input buffer for your next user input.

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by CommonTater View Post
    First... post your code here. Don't make us go chasing it... 'cause we won't.
    All right. I thought the codes were "too long" (70 and 80 lines) to be pasted here, and I didn't want to place just part of them, since I didn't have a clue on where the problem was. I will not repeat that.

    Quote Originally Posted by CommonTater View Post
    Buffered input means that every keystroke... even ones that don't get processed, are stored in an input buffer. At some point you need to clear it out.

    For example, when you have..
    Code:
    int c;
    printf("Enter a letter : ");
    c = getchar();
    so the prompt comes up and you press in A then you hit the <enter> key... BOTH keystrokes go into the buffer, the A is read back by the getchar() leaving the <enter> key behind... Now you need to clear that out before calling your next input function, or it will simply see you pressing <enter>.

    Worse still you could enter "My name is fred and I like to eat spam" and hit enter... The only thing that is assigned is the M, leaving the rest behind.
    I think you explained the issue clearly and accurately. I made some tests and they indeed returned what I expected based on what I learned from you.

    Quote Originally Posted by CommonTater View Post
    The usual method is to loop until you find the newline...
    Code:
    while(getchar() != '\n');
    ... is usually adequate to ensure an empty input buffer for your next user input.
    Thanks for the tip. I'll try to use it more often.

  4. #4
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Good explanation indeed. And i'd be the first to lurk around threads containing the titles "buffered".

    On the topic, what is the buffer size defined by the library used for getchar()? And according to your while statement, are you not merely reaching the end of the buffer, you're not really clearing it? In that case memory will somehow lurk throughout the life of the programme execution?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... this is a keyboard we're talking about... the slowest of all input devices. What are the odds of a typist getting far enough ahead of a Quad core AMD running 2.4ghz to have anything in the buffer after hitting enter? Seriously...

    From stdio.h in Pelles C...
    Code:
    #define BUFSIZ  512
    Yes you can open headers as text files and look this stuff up.
    (Just don't be changing anything!)

    Yes it clears the buffer... the characters are removed from the input queue and assigned to nothing... leaving the input pointer 1 character beyond the newline, and most likely at the EOF marker.
    Last edited by CommonTater; 10-06-2011 at 04:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() doesn't wait for input
    By cantinero74 in forum C Programming
    Replies: 5
    Last Post: 04-27-2010, 09:46 AM
  2. Buffered input
    By BlaX in forum C Programming
    Replies: 10
    Last Post: 02-07-2010, 07:36 AM
  3. understanding user-defined classes
    By Brian_Jones in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2009, 01:58 AM
  4. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  5. Buffered input
    By CeeCee in forum C Programming
    Replies: 12
    Last Post: 11-25-2001, 03:15 AM