Namespace.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <cstdlib>namespace mylib {double readandprocesssum (std: :istream&);} int _tmain (int argc, _tchar* argv[]) {using namespace std;double sum;try {sum = mylib::readandprocesssum (CIN);} catch (const ios::failure& error) {cerr << I/O exception: "<< error.what () << Endl;return Exit_fai LURE;} catch (const exception& error) {Cerr << "standard exception:" << error.what () << Endl;return Exit_f Ailure;} catch (...) {Cerr << "Unknown exception" << Endl;return exit_failure;} Print Sumcout << "sum:" << sum << Endl;} #include <istream>namespace mylib {double readandprocesssum (std::istream& strm) {using std::ios;double value , sum;//save current state of exception flagsios::iostate oldexceptions = Strm.exceptions ();/* Let Failbit and badbit thr ow exceptions*-Note:failbit is also set at End-of-file*/strm.exceptions (ios::fAilbit | Ios::badbit); try {/* while stream was ok*-read value and add it to Sum*/sum = 0;while (strm >> value) {sum + = value ;}} catch (...) {/* If exception not caused by end-of-file*-restore Old state of exception flags*-Rethrow exception*/if (!strm.eof ()) {strm.exceptions (oldexceptions); Restore exception Flagsthrow; rethrow}}//restore Old state of Exception flagsstrm.exceptions (oldexceptions);//return Sumreturn sum;}}
C + + practiced hand