gpt4 book ai didi

C++ strtod 没有正确读取参数类型

转载 作者:行者123 更新时间:2023-11-30 01:03:27 26 4
gpt4 key购买 nike

我正在学习 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)

https://en.cppreference.com/w/cpp/string/basic_string/stof

关于C++ strtod 没有正确读取参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226513/

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