Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ stod function equivalent

Tags:

c++

A friend of mine sent me his program. He uses "stod" function to convert string to double. Is there any other possibility to do such thing?

My complier shows error "stod was not declared in this scope"

I've included #include <string> #include <cstdlib> but nothing has changed.

My complier doesn't use C++11 features. By the way, that program were prepared as a school project, without purpose of using C++11.

like image 538
Antoni Bąk Avatar asked Jul 22 '26 20:07

Antoni Bąk


1 Answers

std::atof() is a quick option handed over from C language, you have to include <cstdlib> to use it.

Otherwise you can use std::stringstream to handle it.

    #include<sstream> 

    std::string value="14.5";
    std::stringstream ss;
    ss<< value;

    double d=0.0;
    ss>>d;
like image 152
Steephen Avatar answered Jul 24 '26 11:07

Steephen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!