// L-6 MCS 360 Fri 3 Sep 2010 : read_integer2.cpp

/* The user is prompted to enter an integer number.
  
Calling the exceptions function of the console input (cin)
passing the error flag ios_base::failbit will cause the exception
ios_base::failure to be thrown when the user enters not an int.

To clear the error flags, we call the clear() function of cin.  */

#include <iostream>
using namespace std;

int main()
{
   int n;

   cin.exceptions(ios_base::failbit);
   do
   {
      cout << "Enter and integer number : ";
      try
      {
         cin >> n; break;
      }
      catch(ios_base::failure)
      {
         cin.clear(); // clear failed state of cin
         cin.ignore(numeric_limits<int>::max(),'\n');
      }
   } 
   while(true);
   cout << "-> your integer : " << n << endl;

   return 0;
}

