Help with Programming Project!

I'm working on a Programming Project from the Absolute C++ book, in Chapter 18. I have the following code, and I need to rewrite it so that it throws an exception when a product is not found instead of returning -1, due to the fact that the caller might ignore the -1, and it may eventually be used as a product id number.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 int get_product_id(int, string, int, string);

 int main() {
     int product_ids[] = {4,5,8,10,13};
     string products[] = {"computer", "flash drive", "mouse", "printer", "camera"};
 
     cout << get_product_id(product_ids, products, 5, "mouse") << endl;
     cout << get_product_id(product_ids, products, 5, "camera") << endl;
     cout << get_product_id(product_ids, products, 5, "laptop") << endl;
 
     return 0;
  }

 int get_product_id(int ids[], string names[], int num_products, string target) {
     for(int i=0; i < num_products; i++) {
        if(names[i] == target) {
           return ids[i];
        }
        return -1; //not found
     }
  }
1. I don't think your return -1; should be inside the for-loop

2. http://www.cplusplus.com/doc/tutorial/exceptions/ :-)
use an if-else to check if found

 
cout << get_product_id(product_ids, products, 5, "mouse") << endl;


instead of using that
use this:
1
2
3
4
5
int id_num = get_product_id(product_ids, products, 5, "mouse")
if( id_num <  0)
    cout << id_num;
else
   cout << "Not found";

or have the return value of the function to bool..
true if found / false if not found then have the id number passed by reference or pointer..
I have been working on this and made it so there is a class called myException. Do you think it will work this way?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 //libraries, macros, ect.
  #include<iostream>
  #include<cstdlib>
  #include<string>
 
  using namespace std;
 
  //class for exception
  class myException {
     public:
        myException (string prod_name) : name (prod_name) {}
        string get_name () {
           return name;
        }
     private:
       string name;
  };
 
  //declare functions
  int get_product_id(int, string, int, string) throw (myException);
 
  int main() {
     int product_ids[] = {4,5,8,10,13};
     string products[] = {"computer", "flash drive", "mouse", "printer", "camera"};
     int total_products;
     string search;
     char answer;
 
     //cout << get_product_id(product_ids, products, 5, "mouse") << endl;
     //cout << get_product_id(product_ids, products, 5, "camera") << endl;
     //cout << get_product_id(product_ids, products, 5, "laptop") << endl;

     try {
        do {
           cout << "Enter the name you want to search: ";
           getline (cin, search);
 
           int prod_id = get_product_id (product_ids, products, total_products, search);
           cout << "The id is: " << prod_id << endl;
           cout << "Do you want to search another product? Y for Yes: ";
           cin >> answer;
        }while(answer=='y'||answer=='Y');
     }
     catch (myException name_not_found) {
        cout << name_not_found.get_name() <<
           " product is not found in products list." << endl;
        exit(1);
     }
  }
 
  int get_product_id(int ids[], string names[],
        int num_products, string target) throw (myException) {
     for(int i=0; i < num_products; i++) {
        if(names[i] == target) {
           return ids[i];
        }
     }
     throw myException(target);
  }
                                              


I'm also getting these two errors -
product_ids.cpp: In function âint main()â:
error: invalid conversion from âint*â to âintâ
error: conversion from âstd::string*â to non-scalar type âstd::stringâ requested
You could create an extension to std::exception and throw an instance of that child upon an error.

As for everything else:

If you need the value of 0 to be reserved, I suggest returning a string and converting your numbers into the string. If an error occurs, then it can return a defined value that represents an error or failure. You can use stringstream to do this.

Also, I would highly recommend using switch statements over if-else statements.
Topic archived. No new replies allowed.