From cppreference.com
voidclear(std::ios_base::iostatestate=std::ios_base::goodbit);Sets the stream error state flags by assigning them the value of state. By default, assigns
which has the effect of clearing all error state flags.
If
is a null pointer (i.e. there is no associated stream buffer), then state|std::ios_base::badbit is assigned.
Parameters
state - new error state flags setting. It can be a combination of the following constants: Constant Explanation
no error
irrecoverable stream error
input/output operation failed (formatting or extraction error)
associated input sequence has reached end-of-file Return value
(none)
Exceptions
If the new error state includes a bit that is also included in the
mask, throws an exception of type
.
Example
clear() without arguments can be used to unset the
after unexpected input; for std::cin.putback(c) see
.
Run this code
#include<iostream>#include<string>intmain(){for(charc:{'\n','4','1','.','3','\n','Z','Y','X'})std::cin.putback(c);// emulate user's input (not portable: see ungetc Notes)doublen;while(std::cout<<"Please, enter a number: "&&!(std::cin>>n)){std::cin.clear();std::stringline;std::getline(std::cin,line);std::cout<<line<<"\nI am sorry, but '"<<line<<"' is not a number\n";}std::cout<<n<<"\nThank you for entering the number "<<n<<'\n';}Output:
Please, enter a number: XYZ I am sorry, but 'XYZ' is not a number Please, enter a number: 3.14 Thank you for entering the number 3.14 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 an excption would be thrown if the current error state
includes a bit that is also included in the
mask checks the new
error state instead See also