A buffer functions as a form of temporary storage. Each standard input and output device features both an input and output buffer. In the realm of traditional C/C++, streams operate with buffering enabled. For instance, when a key is pressed on the keyboard, the input is transmitted to the operating system's buffer where it stays until the program's allocated execution time is complete.
What impact does it have on Programming?
On numerous occasions, it may be necessary to clear out an unwanted buffer to prepare for the next input to be stored in the correct container rather than being inadvertently held in the buffer of the prior variable. For instance, in the C programming language, when handling character arrays or characters, it is crucial to flush the input buffer; in C++, the input buffer needs to be cleared to avoid the risk of the input being stored in the wrong container. Failing to clear the input buffer results in the data being retained in the buffer of the previous variable, causing the subsequent input to be missed by the program when the "Enter" key is pressed, as the buffer meant for the new container remains occupied.
C Program:
// C program to demonstrate why not clearing the input buffer results in undesirable outputs.
#include<stdio.h>
int main ()
{
char str1 [90], ch1;
// user inputs(javaLogic Practice)
scanf ( "%s", str1 );
// user inputs a character(c)
ch1 = getchar();
// printing input(javaLogic Practice)
printf ( "%s\n", str1 );
// Doesnt print (c)
printf ( "%c", ch1 );
return 0;
}
Output: (If user inputs javaLogic Practice c)
javaLogic Practice c
javaLogic Practice
Time Complexity will be: O(1).
C++ Program:
// C++ program to demonstrate why not clearing the input buffer results in undesirable outputs.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int num;
char character[80];
cout<<"Now enter the values"<<endl;
cin >> num;
cin.getline(character,80);
cout<<"This is the generated output"<<endl;
cout << num << endl;
cout << character << endl;
return 0;
}
Output: (If user inputs 20 logic practice)
Now enter the values
20 Logic Practice
This is the generated output
20
Logic Practice
Time Complexity will be: O(1).
The unexpected output results from a Buffer that is currently in use. The presence of the "\n" character in the buffer causes it to be interpreted as part of the subsequent input, leading to the issue.
What is the best way to solve it?
In the case of C:
Enter "while ((getchar) != '\n');" ensures that the buffer characters are read until the end and discarded, including newlines. When applied after a "scanf" statement, it serves to clear the input buffer and allows for input into the intended variable.
C Program:
#include<stdio.h>
int main ()
{
char str1 [90], ch1;
scanf ( "%s", str1 );
while ( (getchar()) != '\n' );
ch1 = getchar ();
printf ( "%s\n", str1 );
printf ( "%c", ch1 );
return 0;
}
Output: (If user inputs javaLogic Practice c)
javaLogic Practice c
javaLogic Practice c
The time complexity is represented as O(n), with 'n' representing the size of the string.
Typing "fflush(stdin)" following a "scanf" function call can also aid in clearing the input buffer. Nevertheless, it is typically disregarded and is deemed "undefined" for input streams according to C++11 standards.
In C++, when you use "cin.ignore(numeric_limits::max,'n');", it will clear the input stream completely, including the newline character, after the "cin" statement.
C++ Program:
#include<iostream>
#include<ios>
#include<limits>
using namespace std;
int main()
{
int number;
char strings[80];
cout<<"Now enter the values"<<endl;
cin >> number;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cin.getline(strings, 80);
cout<<"Here is the output"<<endl;
cout << number << endl;
cout << strings << endl;
return 0;
}
Output:
Now enter the values
5
JTP
Here is the output
5
JTP
Time complexity is constant, denoted as O(1).
Using "cin.sync ": When "cin.sync" is appended to the "cin" command, it clears the buffer entirely. It's important to note that the behavior of "cin.sync" may vary across different programs (as per C++11 and newer standards).
C++ Program:
#include<iostream>
#include<ios>
#include<limits>
using namespace std;
int main()
{
int b;
char st[80];
cout<<"Now enter the values - "<<endl;
cin >> b;
cin.sync();
cin.getline(st, 80);
cout<<endl<<"Here is the output - "<<endl;
cout << b << endl;
cout << st << endl;
return 0;
}
Output:
Now enter the values -
1
This is a program
Here is the output -
1
This is a program
Time Complexity is constant, denoted as O(1).
- By utilizing the "cin >> ws" method, you can direct the compiler to disregard the buffer and strip away any leading whitespaces before accessing the actual content of the string or character array.
C++ program:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
string st;
cout<<"Now enter the values - "<<endl;
cin >> n;
cin >> ws;
getline(cin, st);
cout<<endl<<"Here is the output - "<<endl;
cout << n << endl;
cout << st << endl;
return 0;
}
Output:
Now enter the values -
3
Demo program
Here is the output -
3
Demo program
Time Complexity will be O(1).