Javatpoint Logo
Javatpoint Logo

Clearing the Input Buffer in C/C++

In this tutorial, we will learn what a buffer is and how to clear the input buffer in both C and C++.

What exactly is a Buffer?

A buffer is a type of temporary storage. Every standard input and output device have an input and output buffer. In traditional C/C++, streams are buffered. When we press a key on the keyboard, for example, it is communicated to the operating system's buffer and remained there until the time allotted to that programme ends.

What impact does it have on Programming?

On several occasions, we may need to empty the undesirable buffer in order to receive the next input in the desired container rather than the previous variable's buffer. In C, for example, if we want to input a character array or character, we must empty the input buffer; in C++, we must clear the input buffer; otherwise, the intended input is retained by a buffer of the previous variable, not by the desired container. Because the previous variable's buffer was the space for a new container (because we didn't clear it) when we hit "Enter" (carriage return) on the output screen after the first input, the computer misses the next container input.

C Program:

Output: (If user inputs javaTpoint c)

javaTpoint c
javaTpoint

Time Complexity will be: O(1).

C++ Program:

Output: (If user inputs 20 javatpoint)

Now enter the values
20 Javatpoint
This is the generated output
20
 Javatpoint

Time Complexity will be: O(1).

The output is not generated as expected in either of the preceding codes. This is due to an occupied Buffer. The character "\n" remains in the buffer and is processed as the following input.

What is the best way to solve it?

In the case of C:

Using " while ((getchar()) != '\n'); " : Enter "while ((getchar())!= 'n');" gets to read the buffer characters to the end as well as discards them (including newlines), and when used after the "scanf()" statement, it helps remove the input buffer as well as enables input into the desired container.

C Program:

Output: (If user inputs javaTpoint c)

javaTpoint c
javaTpoint c

Time Complexity will be: O(n), where the size of the string is denoted by n.

Using "fflush(stdin)": Typing "fflush(stdin)" after a "scanf()" command also helps to clear the input buffer, however it is normally ignored and is labelled as "undefined" for input streams in C++11 standards.

In the case of C++:

1. Using "cin.ignore(numeric limits::max(),'n'); ":- After the "cin" statement, putting "cin.ignore(numeric limits::max(),'n');" discards all of the input stream, along with the newline.

C++ Program:

Output:

Now enter the values
5 
JTP
Here is the output
5
JTP

Time complexity will be O(1).

2. Using "cin.sync() ": Typing "cin.sync()" after the "cin" statement removes everything in the buffer. Despite the fact that "cin.sync()" may not operate throughout all applications (according to C++11 and above standards).

C++ Program:

Output:

Now enter the values - 
1 
This is a program

Here is the output -
1
This is a program

Time Complexity will be O(1).

3. Using "cin >> ws": Putting "cin>>ws" after the "cin" instruction instructs the compiler to ignore the buffer and to eliminate all whitespaces before the real content of the string or character array.

C++ program:

Output:

Now enter the values - 
3
Demo program

Here is the output -
3
Demo program

Time Complexity will be O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA