CJCoding With Joseph
15free / daySign inCreate account

Factory That Falls Back on a Bad Temperature

A constructor has no return value to report a problem with, so it throws. A factory function is a good place to catch that and hand back something safe instead:

- explicit Temperature(double celsius): a value below -273.15 throws std::out_of_range("below absolute zero"); otherwise the value is stored - Temperature makeTemperature(double celsius): tries to build a Temperature from the value and returns it; when the constructor throws, it prints invalid reading, using 0 on its own line and returns Temperature(0)

The tests read readings from input, one per line, and print the celsius() of makeTemperature(reading) for each, one per line. For the first test the input is 21.5, -300, -273.15.

Do NOT write a main function.

Expected Output:

21.5
invalid reading, using 0
0
-273.15

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
21.5\n-300\n-273.15\n
This input is automatically fed to your program's stdin
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.