stoi函数在内存位置0x0035E8D8给出错误:std::invalid_argument.c++
stoi function gives error: std::invalid_argument at memory location 0x0035E8D8. c++
程序运行良好,直到它到达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::strtol
或std::strtoll
)将errno
设置为ERANGE
,则使用std::out_of_range
。
因此,此异常取决于您的实际输入,您目前没有从问题中透露(不幸的是)。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- 使用 std::sort 时"invalid operands to binary expression"
- 我越来越"invalid conversion from `void*` to `std::complex<double>*"
- 我在检查字符串时得到"Invalid utf 8 error",但当我使用 std::cout 时似乎是正确的
- "Invalid iterator range"将initializer_list插入 std::vector 时
- GCC vs. Clang:"invalid use of incomplete type" std::d eclval 和模板专业化
- stoi"在抛出'std::invalid argument'实例后终止调用
- Debug assert "Invalid format directive" for std::p ut_time
- std::chrono::time_point invalid value