I would like to make the user input a center number of character, e.g. 10, however, the user might input more than 10.
for(int i = 0 ; i< 10 ; i++)
cin>>x;
The extra character could make my code crash since I will ask for input later.
How can I clear the input at this moment when the user input more than 10?
Thanks so much!
By the way, to avoid duplicating all that code every time, I once wrote a little template function to do that work:
The first overload may be preferred if you want to avoid copying, the second may be more convenient for builtin types.
Usage examples: