gpt4 book ai didi

c++ - strtod 不在错误输入时设置 errno

转载 作者:搜寻专家 更新时间:2023-10-31 00:43:54 26 4
gpt4 key购买 nike

g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

#include <errno.h>
...
cin >> str;
errno = 0 ;
double d = strtod(str.c_str(), NULL);
if (errno) {
cout << "Please, enter number.";
}

输入错误时 errno 保持 0。

编辑:接下来工作正常:

char *err;
double d = strtod(str.c_str(), &err);
if (strlen(err)) {
cout << "Please, enter number." << endl;
}

最佳答案

什么样的“错误输入”?根据联机帮助页,errno 仅在输入的数字太大或太小而无法存储在数据类型中时设置,但不会根本不是一个数字。

If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

If the correct value would cause overflow, plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the sign and type of the return value), and ERANGE is stored in errno. If the correct value would cause underflow, zero is returned and ERANGE is stored in errno.

关于c++ - strtod 不在错误输入时设置 errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com