I\'ve been trying to find the solution for this all day! You might label this as re-post but what I\'m really looking for is a solution without using boost lexical c
Since C++11 you could use std::stod function:
string line;
double lineconverted;
try
{
lineconverted = std::stod(line);
}
catch(std::invalid_argument)
{
// can't convert
}
But solution with std::stringstream
also correct:
#include
#include
#include
int main()
{
std::string str;
std::cin >> str;
std::istringstream iss(str);
double d = 0;
iss >> d;
std::cout << d << std::endl;
return 0;
}