[英]stoi function gives error: std::invalid_argument at memory location 0x0035E8D8. c++
程序運行良好,直到獲得stoi函數,然后程序中斷並給我以下錯誤:“ Microsoft C ++異常:內存位置0x0030EE7C的std :: invalid_argument。” 我看過有關使用stoi的教程,但不確定自己做錯了什么。 平面文件如下所示:
Organic
7
description
light
4
description
menthol
5
description.
每個單詞或數字都換行。
struct ProdDescriptor
{
string name;
string price;
string descript;
};
void getProds() // reads products off of the flat file
{
int array = 3;
ProdDescriptor x[3];
ifstream ItemRead(FlatFileName); // object of the flat file
string temp;
if (ItemRead.is_open()) // opens flat file and reads
{
for (int i = 0; i < array; i++)
{
ProdSpecPrice[i] = 0; // initialize
getline(ItemRead, x[i].name);
getline(ItemRead, x[i].price);
getline(ItemRead, x[i].descript);
temp = x[i].price;
ProdSpecPrice[i] = stoi(temp);
ProdSpecName[i] = x[i].name;
ProdSpecDescription[i] = x[i].descript;
}
從參考文檔開始 ,必須期望std::stoi()
引發以下異常:
例外
std::invalid_argument
如果無法執行轉換,則std::out_of_range
如果轉換后的值超出結果類型的范圍,或者基礎函數(std::strtol
或std::strtoll
)將errno
為ERANGE
。
因此,該例外情況取決於您的實際輸入,但是您目前尚未從問題中得到披露(不幸的是)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.