
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Clearing Input Buffer in C/C++
In C/C++, an input buffer is a temporary storage area where the program processes the input. Suppose, you are an user and you type some characters using a keyboard but those characters are not passed to the program directly because it involves several layers of handling such as keyboard firmware, OS input queues, and event processing. So, they first proceed with the collection of an input buffer. Next, when the program is ready, it reads from the buffer.
How Input Buffer Affects a Program?
The input buffer affected to the program when we are using the functions like scanf() or getchar(). If such buffer are not cleared then unexpected behavior occurs in the program.
Here are some issues due to input buffer in the program:
- Leftover \n (Newline) Characters: When we press the ENTER key button , the \n is stay in the buffer.
- Skips Data Inputs: When scanf() fails, the bad input stored in the buffer. Next, scanf() or getchar() may read the old data instead of reading new data.
- Infinite Loops: When the buffer is not cleared the loops may reads the old data.
Let us see the practical usage of clearing input buffer in the programs:
1. Below the program skips getchar() as \n was already in buffer.
int num; char ch; printf("Enter a number: "); // User enter "10" and the buffer has "10\n" scanf("%d", &num); printf("Enter a character: "); // Reads leftover '\n' instead of waiting for input ch = getchar();
2. To avoid the bugs from the program use either fgets() or sscanf(). So, it will clear the buffer.
// Clears all leftover characters while ((getchar()) != '\n');
Using getchar() and cin.get()
Both these functions are used to clear the input buffer to the programs. The getchar() of C read one character from the input buffer when there is no newline ['\n'] and the loop is reading character continuosly without discard it. The cin.get() of C++ also do the same. Here, it removes the leftover character from the input stream.
Example
In this example, we demonstrates the functions like getchar() [C] and cin.get() [C++] to clear the input buffer.
#include<stdio.h> int main() { char str[80], ch; scanf("%s", str); // Clear input buffer while ((getchar()) != '\n'); ch = getchar(); // display the string printf("%s\n", str); // display the characters printf("%c", ch); return 0; }
Output
The above code allows user to enter the string and character and print the same as follows:
tutorialspoint a
#include <iostream> #include <string> using namespace std; int main() { string str; char ch; // read an input string cin >> str; // remove leftover characters until newline while (cin.get() != '\n'); // read a single character ch = cin.get(); // Print the string cout << str << endl; // Print the character cout << ch; return 0; }
Output
The above code allows user to enter the string and character and print the same as follows:
tutorialspoint a
Using fgets() Function
The fgets() is C standard library that comes under <stdio.h> header. It is used to read a string from a file and store it in buffer input. This reads the space, specified integers or characters, line of text and stops when newline will be found.
Syntax
It's syntax is as follows:
char *fgets(char *str, int n, FILE *stream);
Here,
- str: This is pointer to a character array where the input will be stored.
- n: This maximum number of characters to read, containing the null terminator \0.
- stream: This is an input stream.
Example
In this example, you will see the usage of fgets() function.
#include <stdio.h> int main() { char str[100]; // Read a full line from stdin fgets(str, sizeof(str), stdin); // prints the string including the newline printf("You entered: %s", str); return 0; }
Output
The above program produces the following result:
tutorial You entered: tutorial
Using getline() Function
The getline() of C++ is used to read an entire line of input from a stream (such as cin). It reads a line of text, including spaces, and stops at a newline (\n). It automatically handle the memory positions for the string and doesn't specify the buffer size.
Syntax
It's syntax is as follows:
istream? getline(istream& is, string& str);
Here,
- is: This is the input stream.
- str: This is the string object where the input will be stored.
Example
This is the C++ program to show the usage of getline() function.
#include <iostream> #include <string> using namespace std; int main() { string str; // Read a full line from cin getline(cin, str); cout << "You entered: " << str << endl; return 0; }
Output
The above program produces the following result:
School You entered: School