Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Input Buffer

GadiGadi Member Posts: 5
Hi I am continuing the question about SCANF (by the way thank you Pappy).

Could somebody please explain to me what is the input buffer and does the only way to "clean" it is with while(getchar!='
').

Thank's

Gadi

Comments

  • hondashadowhondashadow Member Posts: 16
    : Hi I am continuing the question about SCANF (by the way thank you Pappy).
    :
    : Could somebody please explain to me what is the input buffer and does the only way to "clean" it is with while(getchar!='
    ').
    :
    : Thank's
    :
    : Gadi
    :
    As far as I know (but I may be wrong) the input buffer stores a couple of characters typed. To empty it you could use
    fflush(stdin);
    before doing your scanf, at least you'll be sure it's empty before the user gives input.

    Joost.

  • Pappy1942Pappy1942 Member Posts: 46
    : Hi I am continuing the question about SCANF (by the way thank you Pappy).
    :
    : Could somebody please explain to me what is the input buffer and does the only way to "clean" it is with while(getchar!='
    ').
    :
    : Thank's
    :
    : Gadi
    :
    Hi again,

    The following is from msdn(MS Developers Network).



    INFO: Next scanf/fscanf Apparently Is Skipped During Run Time
    Last reviewed: September 2, 1997
    Article ID: Q42075

    The information in this article applies to:
    The C Run-time (CRT) included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0


    SUMMARY
    When a function of the scanf() family reads a white-space character--blank (' '), tab (' '), or newline ('
    ')--it does not ordinarily store that character into the location provided by the argument pointer. However, as documented in the Microsoft C Run-Time Library Reference, if the %c type field format specifier is used, the scanf() functions will store a white- space character. This behavior can cause unexpected results.



    MORE INFORMATION
    After the first character is read in the sample code below, the following white-space character that is still in the internal buffer for stdin is read and stored by the second scanf(). This effectively causes the second prompt to be skipped (the message is printed, but the program does not wait to accept a character).

    To obtain the desired behavior, use the format specifier %1s instead of %c. Don't forget to pass scanf() an array of two at least two characters, because the scanf() family will also store a terminating character ('') when "s" is used for the type field format.

    Alternatively, the fflush() function can be used to flush all characters, including white-spaces, out of the specified buffer after each scanf() or fscanf(). Use flushall() to flush all file buffers.



    MORE INFORMATION


    Sample Code

    /* Compile options needed: none
    */

    #include

    void main()
    {
    char a,b[2];
    do
    {
    printf( "Enter a single character
    " );
    scanf( "%c", &a );

    printf( "Enter another character
    " );
    scanf( "%c", b );
    }
    while ( a != 'y' );
    }
    Keywords : CRTIss kbcode kbfasttip
    Version : MS-DOS:6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,4.0,5.0
    Platform : MS-DOS NT WINDOWS
    Issue type : kbinfo



    --------------------------------------------------------------------------------

    ================================================================================


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: September 2, 1997
    1998 Microsoft Corporation. All rights reserved. Terms of Use.


    Pappy
    Pappy
    You learn something everyday.

  • GadiGadi Member Posts: 5
    Thank you both and especially pappy (can I call you 1942 it is sound more personal ;-)). It is the answer I wanted.

    Gadi
Sign In or Register to comment.