SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Exception Handling
Exercise:
Programming Projects
Question:4 | ISBN:9780132846813 | Edition: 5

Question

The following code uses two arrays, one to store products and another to store roduct IDs (a better organization would be to use a single array of a class or struct, but that is not the subject of this Programming Project). The function getProductID takes as input the two arrays, the length of the arrays, and a target product to search for. It then loops through the product name array; if a match is found, it returns the corresponding product ID:

int getProductID( int ids[], string names[], int numProducts, string target)

{

for ( int i=0; i < numProducts; i++)

{

if (names[i] == target)

return ids[i];

}

return -1; // Not found

}


int main() // Sample code to test the getProductID function

{

int productIds[] = {4, 5, 8, 10, 13};

string products[] = {"computer","flash drive",

"mouse","printer","camera"};

cout << getProductID(productIds, products, 5, "mouse") << endl;

cout << getProductID(productIds, products, 5, "camera") << endl;

cout << getProductID(productIds, products, 5, "laptop") << endl;

return 0;

}


One problem with the implementation of the getProductID function is that it returns the special error code of -1 if the target name is not found. The caller might ignore the -1, or later we might actually want to have -1 as a valid product ID number. Rewrite the program so that it throws an appropriate exception when a product is not found instead of returning -1.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Complete Program:

// Header files section
#include <iostream>
#include <string>
using std::string; 
using std::cout; 
using std::endl;

// getProductID function implementation
int getProductID(int ids[], string names[], int numProducts, string target)
{
	for (int i = 0; i < numProducts; i++)
	{
		if (names[i] == target)
			return ids[i];
	}

	// throw an error message
	throw (target + " is not found.");

} // end of getProductID function

// start main function
int main()
{
	int productIds[] = { 4, 5, 8, 10, 13 };
	string products[] = { "computer", "flash drive", "mouse", "printer", "camera" }; 

	// use try/catch to handle the exceptions
	try
	{
		cout << getProductID(productIds, products, 5, "mouse") << endl;
		cout << getProductID(productIds, products, 5, "camera") << endl;
		cout << getProductID(productIds, products, 5, "laptop") << endl;
	}
	catch (string message)
	{
		// print the error message
		cout << "Error: " << message << endl;
	}

	return 0;
} // end of main function

Sample Output:

0 0

Discussions

Post the discussion to improve the above solution.