don't click here

fflush(stdin)

Discussion in 'Technical Discussion' started by saxman, Jun 9, 2009.

  1. saxman

    saxman

    Oldbie Tech Member
    If fflush(stdin) is improper, how come it works but the recommended method while((I = getchar()) != '\n' && I != EOF) doesn't?

    I'm talking specifically about the issues involving keyboard input, where you have to call getchar() or fgets() or whatever more than once to get it to take input.

    Is there a way to get the so-called proper method to work, or would you advise me to continue using the way that seems to work, even though the experts frown on it?


    BTW: I try to avoid C++, so using cin isn't an option here.
     
  2. nineko

    nineko

    I am the Holy Cat Tech Member
    6,308
    486
    63
    italy
    I always used fflush(stdin) and it always worked.
     
  3. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
  4. saxman

    saxman

    Oldbie Tech Member
    I figured that, but why the heck doesn't the recommended method work then? (at least for me... I'm using Mingw)
     
  5. Armada

    Armada

    Sometimes I do things Member
    338
    0
    0
    My best guess is that a stream is never opened for input, so it ends on the EOF check. Of course, my C isn't as good as my PHP (which doesn't deal with compilers, really), but I think it's about the same concept (as I've been reading from streams lately).
     
  6. SANiK

    SANiK

    Tech Member
    413
    0
    16
    While input exists, fill the buffer

    Code (Text):
    1. //Variables
    2. byte input_buffer[INPUT_TOTAL]={0};
    3.  
    4. //Functions
    5. void input_refresh()
    6. {
    7.   memset(&input_buffer, 0, INPUT_TOTAL);
    8.   
    9.   while(kbhit())
    10.   {
    11.     switch(getch())
    12.     {
    13.       case 72:input_buffer[INPUT_UP]=0xFF; break;
    14.       case 80:input_buffer[INPUT_DOWN]=0xFF; break;
    15.       case 75:input_buffer[INPUT_LEFT]=0xFF; break;
    16.       case 77:input_buffer[INPUT_RIGHT]=0xFF; break;
    17.       
    18.       case 'z':case 'Z':input_buffer[INPUT_Z]=0xFF; break;
    19.       case 'x':case 'X':input_buffer[INPUT_X]=0xFF; break;
    20.     }
    21.   }
    22. }
    23.  
    24. dword input_check(byte a_key)
    25. {
    26.   return input_buffer[a_key];
    27. }