// Code derived from Stroustrup's PPP2 book// § 11.4 String streams// -and beginning on p 394#include<fstream>#include<iomanip>#include<iostream>#include<sstream>#include<stdexcept>#include<string>usingnamespacestd;voiderror(stringconst&s){throwruntime_error(s);}voiderror(stringconst&s1,stringconst&s2){error(s1+s2);}//------------------------------------------------------------------------------// if possible, convert characters in s to floating-point valuedoublestr_to_double(strings){istringstreamis{s};// make a stream so we can read from sdoubled;is>>d;if(!is)error("double format error: ",s);returnd;}voidtest()try{[[maybe_unused]]doubled1=str_to_double("12.4");[[maybe_unused]]doubled2=str_to_double("1.234568e+03");[[maybe_unused]]doubled3=str_to_double("twelve point three");// error:}catch(exceptionconst&e){cerr<<"test() error: "<<e.what()<<'\n';}catch(...){cerr<<"Oops: unknown exception!\n";}structTemperature{doubletemp;stringunit;};voidmy_code(stringlabel,Temperaturetemp){// . . .ostringstreamos;// stream for composing a messageos<<setw(8)<<label<<": "<<fixed<<setprecision(5)<<temp.temp<<temp.unit;// exposition only:// someobject.display(Point(100, 100), os.str().c_str());// . . .}// get the next incremental number for a log fileintget_next_number(){staticintn{0};return++n;}intmain()try{test();intseq_no=get_next_number();// get the number of a log fileostringstreamname;name<<"myfile"<<seq_no<<".log";// e.g., myfile17.logofstreamlogfile{name.str()};// e.g., open myfile17.log}catch(exceptionconst&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}