Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /*
  2. Rodrigo Argumedo
  3. Exception Handling: main.cpp
  4. Created on April 23rd, 2017
  5. */
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <iomanip>
  10.  
  11. const double conversion = 2.54;
  12.  
  13. const int inchesInFoot = 12;
  14.  
  15. double getLength();
  16.  
  17. int main() {
  18.     double feet;
  19.     double inches;
  20.     double centimeters;
  21.  
  22.     std::cout << std::fixed << std::showpoint << std::setprecision(2);
  23.  
  24.     std::cout << "Feet: ";
  25.     feet = getLength();
  26.     std::cout << std::endl;
  27.  
  28.     std::cout << "Inches: ";
  29.     inches = getLength();
  30.     std::cout << std::endl;
  31.  
  32.     centimeters = (inchesInFoot * feet + inches) * conversion;
  33.  
  34.     std::cout << "Centimeter: " << centimeters << std::endl;
  35.  
  36.     return 0;
  37. }
  38.  
  39. double getLength() {
  40.     bool done = false;
  41.     double number = 0;
  42.  
  43.     std::string str = "The input stream is in failed state.";
  44.  
  45.     do
  46.     {
  47.         try {
  48.             std::cin >> number;
  49.  
  50.             if (number < 0) {
  51.                 throw std::exception("You entered a invalid number. ");
  52.             }
  53.         }
  54.         catch (std::exception& e) {
  55.             std::cerr << str << std::endl;
  56.             std::cerr << e.what() << std::endl;
  57.         }
  58.     } while (!done);
  59.  
  60.     return number;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement