Thread: clear the input buffer

  1. #1
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195

    clear the input buffer

    in c i would

    while(c == getchar()) != '\n');

    but how do i do this in c++?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    cin.clear();

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cin.clear() just clears the errorbit evaluated by cin.good() and cin.fail(). It doesn't clear the input buffer. In short there is no great way to clear the input buffer that I know of. Some people usie cin.ignore() with large ints. Some people use a loop reading all data in buffer to dummy variable and ignore it (they address the buffer directly with something like rdbuf()).

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you were close....
    this is one way....

    while(getchar() != '\n'); // notice semi colon
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while (cin.rdbuf()->in_avail() > 0) cin.get();
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Hammer, that code doesn't work in CodeWarrior C++.

    Works fine in MSVC++, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by elad
    In short there is no great way to clear the input buffer that I know of.
    That's the only bad thing about C++, IMO.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Sang-drax
    Hammer, that code doesn't work in CodeWarrior C++.

    Works fine in MSVC++, though.
    ... and there was me trusting one of your previous posts (yes, I cut paste that line from here)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Heh, at that time I thought my method was good.

    I discovered the CodeWarrior problem when I tried it yesterday.

    I don't know what the standard has to say about in_avail(), but in CW it always seems to return 0.

    But there must be some way to clear the buffer!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, you can always stick with cin.ingore

    >cin.ignore(INT_MAX, '\n')
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I think one of the problems is different i/o operations rely on the state being in a certain condition (such as whether a '\n' is already present in the buffer or not). But isn't

    while ((c = cin.get())!='\n');

    the equivilent of

    while((c = getchar()) != '\n');
    Joe

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Hammer
    Well, you can always stick with cin.ingore
    How to implement func() so that cin ALWAYS is empty before reading in var2?

    Code:
    void func();
    
    int main()
    {
    
      if (randomcondition)
      {
         int var;
         cout << "var:";
         cin >> var;
      }
    
      func();
    
      int var2
      cout << "var2";
      cin >> var2;
    }

    Assuming the in_avail() method is not standard, here's an imperfect version:
    Code:
    void func()
    {
        if (cin.eof())
            cin.clear();
        else if (cin.fail())
        {
            cin.clear(); 
            cin.ignore(10000,'\n');    
        }
        else if (cin.bad())
            cin.clear();
    }
    It won't work if the user enters "123 456" in the first cin>>
    Last edited by Sang-drax; 10-04-2002 at 06:14 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Originally posted by JoeSixpack
    But isn't

    while ((c = cin.get())!='\n');

    the equivilent of

    while((c = getchar()) != '\n');
    are they equivilent?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Leaving charscters in input buffer
    By pavlosgr in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 02:10 PM
  2. input buffer settings
    By valaris in forum Linux Programming
    Replies: 6
    Last Post: 09-10-2008, 04:04 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 10:05 AM