Thread: How to clear getline() buffer

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    How to clear getline() buffer

    Hello,


    I am capturing a video in my program using OpenCV. If the user presses "Enter" key, I take a picture and ask the user if the picture is okay. If he says "No", I keep capturing the video and wait for another "Enter" input.


    Note: This pressing the "Enter" key happens in a window that OpenCV creates, not in the terminal.


    So if the user says "No", and while capturing video again, if the user types something in the terminal (by accident), then whatever he types gets written to getline()'s buffer and when I call this getline() function for the second time, it reads that input from its buffer.


    How can I prevent this?

    Code:
    string choice;
    int choiceIsOkay = 0;
    
    
    while (1)
    
    
    {
       // Capture video
    
    
       if (waitKey(30) == 13)    // Enter
       {            
          while (choiceIsOkay == 0)
          {
          cout << "Is the picture okay? (Y/N): "; 
          getline(cin, choice);
    
    
          if ((choice == "Y") || (choice == "y"))
          {
          choiceIsOkay = 2;
          }
    
    
          else if ((choice == "N") || (choice == "n"))
          {
          choiceIsOkay = 1;
          }
    
    
          else
          {
          cout << "\nInvalid input\n";
          choiceIsOkay = 0; // Go back and wait for a valid input
          }                
       }
    
    
       if (choiceIsOkay == 2)
       {
       runAlgorithm = 1;
       break;
       }
    
    
       else choiceIsOkay = 0; // Reset the flag
    
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To clear the input buffer, use std::cin.ignore.
    Here's an example on how to clear the input buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clear buffer
    By justins in forum C Programming
    Replies: 5
    Last Post: 05-19-2007, 06:16 AM
  2. Clear Buffer
    By k4z1nh0 in forum C Programming
    Replies: 1
    Last Post: 03-07-2005, 10:01 AM
  3. another way to clear the buffer
    By caroundw5h in forum C Programming
    Replies: 12
    Last Post: 06-20-2004, 08:52 PM
  4. clear the input buffer
    By stautze in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2002, 04:57 AM
  5. Clear buffer
    By jizzer in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2002, 11:02 AM

Tags for this Thread