← Back to Exceptions
Question 760
Parking Fee Refuses Negative Hours
Parking Fee Refuses Negative Hours
Write a function parkingFee(int hours) that returns the cost of parking as a double: 2.5 per hour. A negative number of hours is impossible, so when hours is below 0 the function must throw a std::runtime_error with the message Hours cannot be negative instead of returning anything. A thrown exception leaves the function at once.
The tests call parkingFee with several hour counts, each inside its own try/catch, printing Fee: followed by the amount, or Error: followed by the exception message, one line per call.
Do NOT write a main function.
Expected Output:
Fee: 7.5 Fee: 0 Fee: 2.5
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Exceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.