// Code derived from Stroustrup's PPP2 book// § 10.7.1 Breaking the problem into manageable parts// -and beginning on p 360#include<iostream>#include<locale>#include<stdexcept>#include<string>usingnamespacestd;voiderror(conststrings){throwruntime_error(s);}voiderror(conststrings1,conststrings2){error(s1+s2);}//------------------------------------------------------------------------------voidskip_to_int(){if(cin.fail()){// we found something that wasn’t an integercin.clear();// we’d like to look at the characters nowfor(charch;cin>>ch;){// throw away non-digitsif(isdigit(ch)||ch=='-'){cin.unget();// put the digit back, so that we can read the numberreturn;}}}error("no input");// eof or bad; give up}intmain()try{cout<<"Please enter an integer in the range 1 to 10 (inclusive):\n";intn=0;while(true){if(cin>>n){// we got an integer; now check itif(1<=n&&n<=10)break;// exit loop on good inputcout<<"Sorry "<<n<<" is not in the [1:10] range; please try again\n";}else{cout<<"Sorry, that was not a number; please try again\n";skip_to_int();}}// if we get here n is in [1:10]}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}