stoi函数在内存位置0x0035E8D8给出错误:std::invalid_argument.c++

stoi function gives error: std::invalid_argument at memory location 0x0035E8D8. c++

本文关键字:std invalid c++ argument 错误 出错 函数 内存 位置 0x0035E8D8 stoi      更新时间:2023-10-16

程序运行良好,直到它到达stoi函数,然后程序中断并给我这个错误" Microsoft c++ exception: std::invalid_argument at memory location 0x0030EE7C "。我看过关于使用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::strtolstd::strtoll)将errno设置为ERANGE,则使用std::out_of_range

因此,此异常取决于您的实际输入,您目前没有从问题中透露(不幸的是)。