while ループ内にスイッチがあります。オプション 4 を 3 回呼び出した後、次にスイッチでどちらのケースに入るかを決定する int を入力したときに、プログラムがクラッシュします。なぜそれが起こるのか分かりません。これは、while ループのコードです。
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 回後のみ)、次に istream ファイル内で cin>>op に到達したときにクラッシュします。クラッシュとは、次のエラー メッセージが表示されることを意味します。助けを歓迎します!