我正在学习 Bjarne Stroustrup 的《编程 - 原则与实践》,一个相当简单的练习让我感到难过(这只是第 11 部分中的第 2 部分)。
#include "../../std_lib_facilities.h"
#include <stdlib.h>
//Write a program that consists of a while loop (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating | is entered.
int main()
{
char temp;
char* endptr = 0;
while (temp != '|')
{
double value;
vector<int> values;
for (string temp; values.size() < 2;)
{
cin >> temp;
{
if (temp == "|")
break;
else
value = strtod(temp, &endptr);
values.push_back(value);
}
}
cout << values[0] << "\n" << values[1] << "\n" << "\n";
values.clear();
}
}
Visual Studio 2017 在这里给我一个错误:
else
value = strtod(temp, &endptr);
参数 temp
建议“不存在从“std::string”到“const char *”的合适的转换函数”,这正是我期望 strtod 的函数。
最佳答案
正如您在 C++ 中一样,使用 std::stod()
代替 (C++11)
关于C++ strtod 没有正确读取参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226513/