C + + practiced hand

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.