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

标签 c++

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

相关文章:

c++ - 如何将 C++ 类返回到 NODE.JS

C++11 动态分配可变长度多维数组

c++ - 为什么从 char* 到 LPCWSTR 的自动转换仅在未设置字符集的情况下有效

c++ - QCache 和 std::shared_ptr

c++ - vs2013 sqlite3错误C4703

c++ - 为什么在转换/复制 vector 时进行这么多复制

c++ - 在 GDI+ 中以特定角度绘制字符串时 AntiAlias 失败

c++ - 如何使用 std::atomic<>

C++:基类调用自己的虚函数——一种反模式?

c++ - 删除 vector 中的字符串