// Code derived from Stroustrup's PPP2 book// § 10.7.2 Separating dialog from function// -and beginning on p 363#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 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}intget_int(){intn=0;while(true){if(cin>>n)returnn;cout<<"Sorry, that was not a number; please try again\n";skip_to_int();}}intget_int(intlow,inthigh,conststring&greeting,conststring&sorry){cout<<greeting<<": ["<<low<<':'<<high<<"]\n";while(true){intn=get_int();if(low<=n&&n<=high)returnn;cout<<sorry<<": ["<<low<<':'<<high<<"]\n";}}intmain()try{intstrength=get_int(1,10,"enter strength","Not in range, try again");cout<<"strength: "<<strength<<'\n';intaltitude=get_int(0,50000,"Please enter altitude in feet","Not in range, please try again");cout<<"altitude: "<<altitude<<"f above sea level\n";}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}