1
0

cinexcp.cpp 714 B

1234567891011121314151617181920212223242526272829303132333435
  1. // cinexcp.cpp -- having cin throw an exception
  2. #include <iostream>
  3. #include <exception>
  4. int main()
  5. {
  6. using namespace std;
  7. // have failbit cause an exception to be thrown
  8. cin.exceptions(ios_base::failbit);
  9. cout << "Enter numbers: ";
  10. int sum = 0;
  11. int input;
  12. try {
  13. while (cin >> input)
  14. {
  15. sum += input;
  16. }
  17. } catch(ios_base::failure & bf)
  18. {
  19. cout << bf.what() << endl;
  20. cout << "O! the horror!\n";
  21. }
  22. cout << "Last value entered = " << input << endl;
  23. cout << "Sum = " << sum << endl;
  24. /* keeping output window open */
  25. /*
  26. cin.clear();
  27. while (cin.get() != '\n')
  28. continue;
  29. cin.get();
  30. */
  31. return 0;
  32. }