0

我正在研究这个 C++ 程序。我需要使用 try throw catch 异常处理。我的程序编译。但是,它返回mouse未找到。事实上,它应该是laptop不应该被发现的词。我已将throw代码从循环内移到for循环外for。但这并没有将结果固定为预期的那样。在函数中包含 throw 代码似乎最合乎逻辑getProductID(),但也许它应该在程序的另一部分?

#include<iostream>
#include<string>

using namespace std;

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

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(string str)
 {
      cout<<"Error: "<< str<< " product not found."<< endl;
      cout<<"End of program."<<endl;

      return 0;
 }


 return 0;

 }
4

3 回答 3

5

替换1i遍历数组中的所有项目:

if (names[1] == target)
          ^
于 2013-11-09T20:08:34.770 回答
1

您还可以使用 C++ 标准工具使您的代码更有效。最好制作一个结构来将 id 及其名称存储在一个单元中:

struct Product
{
    int id;
    std::string name;
};

另外,您最好将throw异常派生自std::exception

struct TargetNotFound : public std::exception
{
    TargetNotFound(const std::string &target) : target(target) {}

    std::string getTarget() const
    {
        return target;
    }

private:
    const std::string target;
};

而且,您可以使用std::vectorandstd::find_if代替循环:

int getProductID(const std::vector<Product> &p, const std::string &target)
{
    auto i = std::find_if(p.begin(), p.end(), [&](const Product &x)
    {
        return x.name==target;
    });

    if (i == p.end())
        throw TargetNotFound(target);
    else
        return i->id;
}

最后,您的主要内容可以是:

int main()
{

    std::vector<Product> products
    {
        {4, "computers"},
        {5, "flash drive"},
        {8, "mouse"},
        {10, "printer"},
        {13, "camera"},
    };

    try
    {
        cout<< getProductID(products, "mouse")<<endl;
        cout<< getProductID(products, "camera")<<endl;
        cout<<getProductID(products, "laptop")<<endl;
    }
    catch(const TargetNotFound &ex)
    {
        cout<<"Error: "<< ex.getTarget() << " product not found."<< endl;
        cout<<"End of program."<<endl;
    }
}

实时代码

于 2013-11-09T20:34:12.143 回答
0
#include<iostream>
#include<string>

using namespace std;

int getProductID(int ids[], string names[], int numProducts, string target)
{
   for(int i=0; i< numProducts; i++)
   {
          if (names[1] == target)
              `return ids[i];`
   }
  throw(target);
}

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;//once exception is     
    //throw by this function call it goes to catch block after that it does not execute      
    //following line
    cout<< getProductID(productIds, products, 5, "camera")<<endl;
    cout<<getProductID(productIds, products, 5, "laptop")<<endl;
 }
catch(string str)
{
     cout<<"Error: "<< str<< " product not found."<< endl;
     cout<<"End of program."<<endl;

     return 0;
}


return 0;

}

一旦通过以下函数调用引发异常,它就会进入 catch 块,之后它不会执行其他行 cout<< getProductID(productIds, products, 5, "mouse")<<endl;

于 2013-11-09T20:23:11.850 回答