istream 在切换循环中崩溃
istream crash in while-switch loop
我在 while 循环中有一个开关。在我调用选项 4 三次后,程序在下次我输入 int 时崩溃,该 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));
}
在三次进入案例 4 后(并且仅在案例 4 之后,仅在 3 次后),下次到达 istream 文件内的 cin>>op 时崩溃。崩溃,我的意思是弹出以下错误消息:"Ex6 中0x4a34870c未处理的异常.exe:0xC0000005:访问冲突。欢迎帮助!
这个:
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*
不是动态分配的,它只是指向异常中内部分配的字符串的指针。不得删除该指针。您的代码中可能还有其他一些错误,但您应该修复此问题。
相关文章:
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- For 循环在尝试读取数组 c++ 时程序崩溃
- 在 mmap'ed 区域上使用 memcpy 崩溃,for 循环不会
- C++代码崩溃并具有无限循环
- 程序因 While 循环而崩溃
- 当通过tcp接收编码图像的消息时,zmq在第二个循环中崩溃
- 递归导致程序崩溃,但 while 循环中的相同概念不是来自C++析构函数
- 我需要做一个空函数循环回来,而不会让它崩溃或跳过
- 布雷森汉姆线抽屉在渲染任何内容之前经过一定循环后崩溃
- 循环导致文件崩溃
- 使用自动循环时代码崩溃
- std::在循环中计算重复字符时擦除崩溃
- 当循环崩溃时,不向数组添加单词
- 程序在循环执行后崩溃
- 控制台在结束时崩溃(循环)未超出限制错误
- 这个循环如何使我的程序崩溃
- 模态qprogressdialog :: setValue()导致嵌套事件循环崩溃
- 文字背景颜色更改后C 中的许多循环崩溃!为什么
- 对于循环,大量崩溃了
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误