hi i dont know how and why use the .clear() function
ie:
please any help about the when and why to use it?Code:file.clear();//if file is a fstream or
cin.clear();//working with cin
thanks for any help
Printable View
hi i dont know how and why use the .clear() function
ie:
please any help about the when and why to use it?Code:file.clear();//if file is a fstream or
cin.clear();//working with cin
thanks for any help
you'd want to use it if you know there is garabage in you streams eg.
someone inputs a letter instead of a number.
http://cplusplus.com/ref/iostream/ios/clear.html
I use it the most when I read EOF yet need to loop back and read again. At that point i have to call clear() that will allow me to read, otherwise it won't.
ok thanks for reply
expanding on what alphaoide said... it resets (clears) all the flags that have been set for that stream. for example, to set cout to only display two decimal points on floats:
and if you wroteCode:cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout<<setprecision(2);
it would clear all the flags you just set.Code:cout.clear()
note: code not tested... I haven't used it in a long time and am not sure it works... you can try if you like... it would probably be good practice with flags... and here's a list of flags and their manipulators: http://cppreference.com/cppio_flags.html