1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include<iostream>
#include<string>
using namespace std;
int getProductID(int ids[], string names[], int numProducts, string target)throw(target)
{
for(int i=0; i< numProducts; i++)
{
if (names[1] != target)
throw(target);
return ids[i];
}
}
int main() //Sample code to test the getProductID function
{
int productIds[] = {4,5,8,10,13};
string products[] = {"computer", "flash drive","mouse","printer","camera"};
try
{
cout<< getProductID(productIds, products, 5, "mouse")<<endl;
cout<< getProductID(productIds, products, 5, "camera")<<endl;
cout<<getProductID(productIds, products, 5, "laptop")<<endl;
}
catch(target)
{
cout<<"Error: Product not found."<< endl;
cout<<"End of program."<<endl;
return 0;
}
return 0;
}
|