0

私はこのC++プログラムに取り組んでいます。try throw catch 例外処理を使用する必要があります。私のプログラムはコンパイルされます。ただし、mouse見つかりませんでした。実際にはlaptop見つからないはずの単語であるべき場合。throwコードをループ内からforループ外に移動しましたfor。しかし、これは結果を期待どおりに修正しませんでした。関数内にスロー コードを配置するのが最も論理的と思わ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

配列内のすべての項目を反復するには、次のように1置き換えます。i

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::vectorandを使用できます。std::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 に答える