Implementation of the getproductid function is that it


The following code uses two arrays, one to store products and another to store product IDs. 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:

intgetProductID(int ids[], string names [],
intnumProducts, string target)
{
for (inti=0; i{
if (names[i] == target)
return ids[i];
}
return -1; // Not found

int main() // Sample code to test the getProductID function
{
intproductIds[] = { 4, 5, 8, 10, 13 };
string products[] = { "computer", "flash drive",
"mouse", "printer", "camera" };
cout<cout<cout<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.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Implementation of the getproductid function is that it
Reference No:- TGS0810528

Expected delivery within 24 Hours