2013-11-09 26 views
0

我正在研究這個C++程序。我需要使用try throw catch異常處理。我的程序編譯。但是,它返回mouse未找到。事實上,它應該是字laptop,不應該被發現。我已將循環中的throw代碼移至for循環之外。但這並沒有解決結果如預期的那樣。在函數getProductID()中使用throw代碼似乎是最合理的,但也許它應該在程序的另一部分?C++試試拋出Catch函數嗎?

#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; 

} 
+1

使用'的std :: find'。它也會神奇地修復你的代碼。你還應該避免使用原始數組,特別是在將它們傳遞給函數時。 'std :: array'知道它的大小並且執行效率一樣。 – chris

+2

請拋出一些std :: exception - 拋出std :: string是醜陋的 –

+0

感謝Dieter的提示。我對學習這一點很陌生。你可以舉個例子拋出std :: exception拋出std :: string嗎? – user2972340

回答

5

通過i更換1所有項目遍歷數組中:

if (names[1] == target) 
     ^
+0

謝謝M M. Typo,我忽略了.... – user2972340

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;

1

您還可以使用C++標準工具使您的代碼更有效。這是很好的做一個結構來存儲ID,其名稱在一個單元:

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

此外,你最好從std::exception衍生更好地throw例外:

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

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

private: 
    const std::string target; 
}; 

而且,你可以使用std::vectorstd::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; 
    } 
} 

Live code