Write a simple program using function that throws an exception if divide by zero occur |
Your code has nothing to do with your exercise requirements.
1)
Do start by writing
one function which checks two numbers before performing a division with them.
- In case the divisor is zero, the function must throw.
- Otherwise, it must return the result.
Call that function from main() and catch its exception.
End of first exercise.
2)
Write 4 functions. Each of them must throw an exception of a specific type (integer, float, string, character...).
Call them from main() and catch their exceptions.
Please note: the exercise doesn’t say those functions should do something canny: they just need to throw.
(Anyway, if they do something canny, maybe you get an higher mark - I don’t know your teacher.)
End of exercise two.
A friend of mine is telling me that there is function (cin.fail) which returns zero if integer is not entered! |
Awesome. And… ?
Anyway, here it is: http://www.cplusplus.com/reference/ios/ios/fail/
- - -
Aside note: when you are required to use a
floating point type, it doesn’t mean you need to use exactly ‘float’.