cinexcp.cpp

来自「C++ Primer Plus(第五版)第3章例题代码」· C++ 代码 · 共 30 行

CPP
30
字号
// cinexcp.cpp
#include <iostream>
#include <exception>
using namespace std;

int main()
{
    // have failbit cause an exception to be thown
    cin.exceptions(ios_base::failbit);
    cout.precision(2);
    cout << showpoint << fixed;
    cout << "Enter numbers: ";
    double sum = 0.0;
    double input;
    try {
	    while (cin >> input)
	    {
	        sum += input;
	    }
    } catch(ios_base::failure & bf)
    {
        cout << bf.what() << endl;
        cout << "O! the horror!\n";
    }

    cout << "Last value entered = " << input << "\n";
    cout << "Sum = " << sum << "\n";
    return 0; 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?