Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rodrigo Argumedo
- Exception Handling: main.cpp
- Created on April 23rd, 2017
- */
- #include <iostream>
- #include <string>
- #include <iomanip>
- const double conversion = 2.54;
- const int inchesInFoot = 12;
- double getLength();
- int main() {
- double feet;
- double inches;
- double centimeters;
- std::cout << std::fixed << std::showpoint << std::setprecision(2);
- std::cout << "Feet: ";
- feet = getLength();
- std::cout << std::endl;
- std::cout << "Inches: ";
- inches = getLength();
- std::cout << std::endl;
- centimeters = (inchesInFoot * feet + inches) * conversion;
- std::cout << "Centimeter: " << centimeters << std::endl;
- return 0;
- }
- double getLength() {
- bool done = false;
- double number = 0;
- std::string str = "The input stream is in failed state.";
- do
- {
- try {
- std::cin >> number;
- if (number < 0) {
- throw std::exception("You entered a invalid number. ");
- }
- }
- catch (std::exception& e) {
- std::cerr << str << std::endl;
- std::cerr << e.what() << std::endl;
- }
- } while (!done);
- return number;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement