// Code derived from Stroustrup's PPP2 book// § 10.7.1 Breaking the problem into manageable parts// -and beginning on p 361#include<cctype>#include<iostream>#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 charactersfor(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}// read an int from cinintget_int(){intn=0;while(true){if(cin>>n)returnn;cout<<"Sorry, that was not a number; please try again\n";skip_to_int();}}// read an int in [low:high] from cin// the range-checking get_int()intget_int(intlow,inthigh){cout<<"Please enter an integer in the range "<<low<<" to "<<high<<" (inclusive):\n";while(true){intn=get_int();if(low<=n&&n<=high)returnn;cout<<"Sorry "<<n<<" is not in the ["<<low<<':'<<high<<"] range; please try again\n";}}intmain()try{intn=get_int(1,10);cout<<"n: "<<n<<'\n';intm=get_int(2,300);cout<<"m: "<<m<<'\n';}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}