1: // Created by Frank M. Carrano and Timothy M. Henry.
2: // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
4: // Listing C3-4
6: // Create and initialize an array of boxes
7: PlainBox<std::string> myBoxes[5]; // Array of PlainBox objects
8: myBoxes[0] = PlainBox<std::string>("ring");
9: myBoxes[1] = PlainBox<std::string>("hat");
10: myBoxes[2] = PlainBox<std::string>("shirt");
11: myBoxes[3] = PlainBox<std::string>("sock");
12: myBoxes[4] = PlainBox<std::string>("shoe");
13: PlainBox<std::string> foundBox;
14:
15: // Try to find a box containing glasses
16: try
17: {
18: foundBox = findBox(myBoxes, 5, "glasses");
19: }
20: catch(std::logic_error logErr)
21: {
22: std::cout << logErr.what() << std::endl; // Display error message
23: foundBox = PlainBox<std::string>("nothing"); // Fix problem
24: } // end try-catch
25: // Because we catch the exception and fix the problem, the following
26: // statement should work even if the target is not found
27: std::cout << foundBox.getItem() << std::endl;