Hello, I'm trying to write a function that will clear and ignore cin data with a single call. Is it possible to use cin.(insert function name) with the following code, without construction? For example cin.flush_cin(some_string_here)?
int main()
{
int i ;
if( std::cin >> i ) std::cout << i << '\n' ;
else
{
std::cerr << "error in input\n" ;
std::cin >> clear_line >> i ; // clear the error, discard rest of line, and then read i
std::cout << i << '\n' ;
}
}
Is it possible to use cin.(insert function name) with the following code, ...
No. Or at least not easily.
cin already has a type. So while you could replace the standard cin with your custom version, your problem would be better handled by writing a utility function instead.
(Apart from anything, the class name flush is very poor. iostream_with_flush would at least same what it is and does. And flush() already has a meaning as defined by ostream::flush())
Then bear in mind that ignore() is actually a method of istream, not iostream (which inherits from ostream and istream) and that they are all actually template instantiations (of basic_istream<>, etc.)
over and over. I knew that i'd need a template to do this, but I'm a bit of a noob at writing them. Our class hasn't learned them yet, so I'm jumping ahead a little.
If the functionality is required only for standard narrow input streams with default character traits, the manipulator need not be templated. This would suffice: