cin.clear() function in C++
Last Updated :
22 Aug, 2024
The cin.clear() function in C++ is a member function of the std::istream class, which is used to clear the error flags on the cin stream. When an input operation fails (for example, due to incorrect data type input), the cin stream enters a fail state, and further input operations are blocked until the error state is cleared. The cin.clear() function resets the error flags, allowing the cin stream to be used again for input.
In this article, we will learn about the cin.clear() function, its syntax, and practical examples to demonstrate its importance in input handling.
Syntax of cin.clear() in C++
cin.clear();
Here, std::cin refers to the standard input stream object.
Parameters of cin.clear()
The cin.clear() function does not take any parameters.
Return Value of cin.clear()
The cin.clear() function does not return any value. Its main purpose is to reset the error state flags of the cin stream.
How cin.clear() Function Works?
When cin encounters an input error, such as trying to read a string where an integer is expected, it sets an internal error flag that indicates the type of error that occurred. Common error flags include:
- failbit: Indicates a logical error on the input operation, such as when the user enters a letter instead of a number.
- badbit: Indicates a more serious error, such as a hardware failure.
- eofbit: Indicates that the end of the input stream has been reached.
Once an error flag is set, cin stops accepting input. The cin.clear() function is used to reset these error flags, effectively telling cin to forget about the error and continue accepting input.
Examples of cin.clear() in C++
The below examples demonstrates how to use the cin.clear() function to handle input errors and allow the user to re-enter correct input in C++.
Example 1: Handling Invalid Input
C++
// C++ program to use the cin.clear() function for validating
//user input as a number and handle invalid input.
#include <iostream>
using namespace std;
int main(){
int number;
cout << "Enter a number: ";
cin >> number;
// Check if the input is valid
while (cin.fail()){
// Clear the error state of cin
cin.clear();
// Ignore the invalid input in the buffer
cin.ignore(1000, '\n');
cout << "Invalid input. Please enter a number: ";
cin >> number;
}
cout << "You entered: " << number << endl;
return 0;
Output
Enter a number: gfg
Invalid input. Please enter a number: 5
You entered: 5
Time Complexity: is O(1) as cin.clear() function simply resets the error flags on the cin stream.
Auxiliary Space: O(1) as it does not involve any additional memory allocation.
Example 2: Clearing the Stream After an Error
C++
// C++ program to use the cin.clear() to read and
//validate age input and then read a name using getline.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
string name;
// Prompt the user to enter their age
cout << "Enter your age: ";
cin >> age;
// Check if the input for age is valid
if (cin.fail()) {
// Clear the error state of cin
cin.clear();
// Ignore any invalid input left in the buffer
cin.ignore(1000, '\n');
cout << "Invalid input for age. Please try again." << endl;
return 1;
}
// Prompt the user to enter their name
cout << "Enter your name: ";
// Clear the newline character left in the buffer
cin.ignore();
// Read the full line for the name
getline(cin, name);
// Display the entered age and name
cout << "Age: " << age << ", Name: " << name << endl;
return 0;
}
Output
Enter your age: 10
Enter your name: geek
Age: 10, Name: geek
Time Complexity: is O(1) as cin.clear() function simply resets the error flags on the cin stream.
Auxiliary Space: O(1) as it does not involve any additional memory allocation.
Similar Reads
cin.ignore() Function in C++ The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input o
3 min read
erase_if() Function in C++ The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and
3 min read
How to Clear a Stack in C++? In C++, clearing a stack means removing all element from the stack container leaving it empty. In this article, we will learn how to clear a stack in C++.The most efficient method to clear a stack is by assigning the new empty stack to our original stack container. Let's take a look at the code exam
3 min read
How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the
2 min read
std::fstream::close() in C++ Files play an important role in programming. It allows storage of data permanently. The C++ language provides a mechanism to store the output of a program in a file and browse from a file on the disk. This mechanism is termed file handling. In order to perform file handling, some general functions w
4 min read