Clear Input Buffer in C++

This article will demonstrate multiple methods of how to clear input buffer in C++.

Use the std::cin::ignore Function to Clear Input Buffer in C++

ignore is a built-in function of std::cin object, and it can be used to extract and discard characters from the input stream until the given delimiter. Before we discuss the behavior of the cin::ignore function, it should be noted that standard input and output streams as cin/cout are buffered, which means that there is some bookkeeping happening between the user writes/reads and the actual buffer state in the operating system kernel. This buffer is managed by the C++ library itself, and its purpose is to make read/write requests more efficient. Note that calling for operating system services for each byte would be a quite inefficient use of CPU resources, so the intermediary buffer is used to accumulate some requests and then call the operating system with a single request.

Thus, cin::ignore can discard leftover bytes in the buffer that may be accidentally read when there are consecutive calls. The following example code is designed to demonstrate the ignore behavior more clearly. We implemented an infinite while loop that reads three integers from the cin stream and stores them in the corresponding variables. The integers are printed to the cout and then the routine is executed on the next iteration. Notice that the user may provide more than three integers on the first iteration. In that case, the code before cin.ignore would store only 3 of them, and others will still exist in an intermediary buffer. Now, if cin.ignore is not called with the given parameters, then the next iteration cin >> line will read the remainder of the input and the inconsistent behavior will occur. cin.ignore call can be utilized in such scenarios to discard the input until the given character is not encountered. In this case, we specified a new line character which is generally used to buffer cin stream.

#include <iostream>
#include <sstream>
#include <limits>

using std::cout; using std::endl;
using std::cin; using std::numeric_limits;

int main() {
    while (true) {
        int i1, i2, i3;
        cout << "Type space separated numbers: " << endl;
        cin >> i1 >> i2 >> i3;
        if (i1 == 0) exit(EXIT_SUCCESS);
        cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
        cout << i1 << "; " << i2 << "; " << i3 << endl;
    }

    return EXIT_SUCCESS;
}

Output (with the sample user input):

Type space separated numbers:
12 32 54
12; 32; 54

Alternatively, we can implement another example code that takes two strings split by space character delimiter. In this scenario, there’s a chance that the user provides more than 2 names which would force this code to drop all initials after 2 space-separated strings. If there are only one name and a space in the user input, then a newline character, which always terminates the cin input, will be stored as the second initial. The latter behavior can be demonstrated by adding the single character output after << name << surname statement.

#include <iostream>
#include <sstream>
#include <limits>

using std::cout; using std::endl;
using std::cin; using std::numeric_limits;

int main() {

    char name, surname;
    cout << "Type your name and surname: " ;
    name = cin.get();
    cin.ignore(numeric_limits<std::streamsize>::max(), ' ');
    surname = cin.get();

    cout << "Your initials are: "<< name << surname << endl;

    return EXIT_SUCCESS;
}

Output (with the sample user input):

`bash Type your name and surname: Lama Lime Your initials are: LL

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ IO

  • Use cin.fail Method in C++
  • Validate User Input in C++