2012-06-16 56 views
1

我在while循环中有一个开关。在我调用选项4三次之后,下一次我输入决定在交换机中进入哪种情况的int时,程序崩溃。我不知道为什么会发生。 这是while循环的代码:istream在while-switch循环中崩溃

void Menu::start() 
{ 
    Store st; 
    int op=1,num,quantity; 
    string name; 
    while(op!=0) 
    { 
     cin>>op; 
     try 
     { 
      switch(op) 
      { 
        case 1: 
      { 
       cin>>num>>name; 
       st.addProduct(num,name); 
       break; 
      } 
      case 4: 
       { 
        cin>>num>>quantity; 
        st.sellProduct(num,quantity); 
        break; 
       } 
      case 0: 
       break; 
      default: 
       throw(exception("Unknown option, try again.\n")); 
      } //end of switch 
     } //end of try 
//catches 
    } //end of while 
} 

/***************************************************************************** 
* function name: addProduct 
* The Input: This Store, const& int num, const& string name 
* The output: If product with given num doesn't exist in store, adds it to 
* store. 
* The Function operation: uses the products map. 
*****************************************************************************/ 
void Store::addProduct(const int& num,const string& name) 
{ 
    //if product doesn't exist in map, add it 
    if(prods.find(num)==prods.end()) 
     prods.insert(pair<int,Product>(num,Product(num,name))); 
    //otherwise issue an error 
    else 
     throw(AddProdException(num)); 
} 

/***************************************************************************** 
* function name: sellProduct 
* The Input: This Store, const int& prodNum, const unsigned int& quantityBought 
* The output: If product doesn't exist or quantityBought is more than 10 units 
* more than quantity in stock, issues an error. Otherwise, sells the product 
* and if needed, issues a shipment such that after the purchase the store will 
* be left with 20 units. 
* The Function operation: uses the products and orders map. 
*****************************************************************************/ 
void Store::sellProduct(const int& prodNum, const unsigned int& quantityBought) 
{ 
    if(prods.find(prodNum)!=prods.end()) 
    { 
     Product& pr = prods.find(prodNum)->second; 
     const int& signedQB=quantityBought, signedPQ=pr.getQuantity(); 
     if(signedPQ<signedQB-10) 
      //store can't supply product 
      throw(BuyQuanException(prodNum,quantityBought)); 
     //make purchase 
     else 
     { 
      //purchase only what left in stock 
      if(signedPQ<signedQB) 
      { 
       //issue shipment 
       Order order=Order(prodNum,20+quantityBought-pr.getQuantity()); 
       orders.insert(pair<int,Order>(order.getID(),order)); 
       //document order 
       purchaseDocs.add(new Documentation(pr,quantityBought, 
        orders.find(order.getID())->second)); 
       //buy product 
       pr.decreaseQuantity(pr.getQuantity()); 
      } 
      //purchase requested amount 
      else 
      { 
       //buy product 
       pr.decreaseQuantity(quantityBought); 
       //document order 
       purchaseDocs.add(new Documentation(pr,quantityBought)); 
      } 
     } //else regarding making the purchase 

    } //if regarding found the product 
    //otherwise issue an error 
    else 
     throw(BuyProdException(prodNum)); 
} 

3后进入到外壳4,(并仅在壳体4中,仅后3次),它崩溃下一个到达CIN >>运算时间,内部的istream文件。通过崩溃,我的意思是弹出以下错误消息:“Ex6.exe中0x4a34870c未处理的异常:0xC0000005:访问冲突。”帮助将受到欢迎!

+0

只是猜测,你没有使用C + + 11,你?无论如何,你应该[valgrind](http://valgrind.org)看看它。 –

+0

“崩溃”是什么意思?此外,请在将代码发布到此处之前尽量减少代码,并为我们提供最小*可编译*代码。您发布的代码包含大量我们不知道的内容,所以我们无法重现您的问题。 –

+0

已更新的问题。乔纳斯,不使用c + + 11。 – nodwj

回答

3

此:

const char* errStr=e.what(); 
cout<<errStr; 
//errStr is a dynamically allocated string we don't need anymore <----------- 
delete[] errStr; 

是一个坏的假设。由std::exception::what返回的const char*不是动态分配的,它只是一个指向异常内部分配的字符串的指针。你一定不能删除那个指针。您的代码中可能存在其他一些错误,但您应该修复此问题。

+0

我明白了你的观点,但只是在我的异常(ProductException)中,errStr是动态分配的 - 所以我必须将其删除以防止内存泄漏...不是吗? – nodwj

+0

您应该提供该例外的定义。我假设你的异常继承了'std :: exception'(从你调用'ProductException :: what()'后看起来是合乎逻辑的)。 – mfontanini

+2

@Idan:在这种情况下,请确保您的例外是可复制的(并在复制时正常工作)。 – ybungalobill