Skip to content

Commit

Permalink
Update ex5_25.cpp
Browse files Browse the repository at this point in the history
Doing many works in catch scope is not a good habit.
  • Loading branch information
pezy committed Oct 16, 2014
1 parent 2e99838 commit 626d319
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions ch05/ex5_25.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
#include <iostream>
#include <stdexcept>

using namespace std;
using std::cin; using std::cout; using std::endl; using std::runtime_error;

int main(void)
{
int a, b;
cout << "Enter two numbers :" << endl;

cout << "Input two integers: ";
while (cin >> a >> b)
{
try{
if (b == 0)
throw runtime_error("divisor is 0");
cout << static_cast<double>(a) / b << endl;
break;
}
catch (runtime_error err){
cout << err.what()
<< "\nTry again ? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break;
else
{
cout << "Enter two numbers :" << endl;
}
}
try {
if (b == 0) throw runtime_error("divisor is 0");
cout << static_cast<double>(a) / b << endl;
cout << "Input two integers: ";
} catch (runtime_error err) {
cout << err.what() << "\n"
<< "Try again.\nInput two integers: ";
}
}

return 0;
Expand Down

0 comments on commit 626d319

Please sign in to comment.