← Back to Exceptions
Question 764
Price Every Item and Skip the Unknown Ones
Price Every Item and Skip the Unknown Ones
The function priceOf(const std::string& item) is already written: it returns the price of apple, bread or milk, and throws a std::runtime_error for any other item. Write priceList(const std::vector<std::string>& items), which goes through the list in order with ONE try/catch per item: print the item name, : and its price, and add the price to a running total; when priceOf throws, catch the std::runtime_error, print Error: followed by its message, and move on to the next item. After the loop print Total: followed by the total of the items that had a price. Putting the try inside the loop is what lets one bad item not cancel the rest.
Do NOT write a main function.
Expected Output:
apple: 0.5 Error: Unknown item: soap milk: 1.75 Total: 2.25
= 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.