【发布时间】:2020-03-16 17:37:12
【问题描述】:
我有一个简单的函数,它接受一串数字并将它们转换为浮点数,函数在下面:
float convertStrToFloatNumber (std::string n) {
// Converts the number n to a float in the form of 000.000
if (6 - n.length() > 0) {
// The representation is with trailing zeros
n.append(6 - n.length(), '0');
}
// Insert . at the 3. spot
n.insert(3, ".");
return strtof((n).c_str(), 0);
}
当我使用输入 "030000" 运行时,我如何抛出以下异常:
“在抛出 'std::length_error' what() 的实例后调用终止:basic_string_M_repalce_aux” 我真正感兴趣的是了解错误的最后一部分是什么意思。
【问题讨论】:
-
你能给我们足够的代码来复制错误吗?我构造了一个
main,我认为它会复制错误,但它运行得非常好。 -
请稍等,显然源文件更大(G 代码解析器)
-
长度不是字符数吗?
-
@MartinSigNørbjerg 长度是一个无符号整数,当你“通过”零时,结果会回绕到最大值。
-
@DavidSchwartz 代码上传到 github 仓库:github.com/Collatz124/GC-TSM
标签: c++ string floating-point