5

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 cast. A traditional C++ way of doing it would be great. I tried this code but it returns a set of gibberish numbers and letters.

string line; 
double lineconverted;

istringstream buffer(line);
lineconverted;
buffer >> lineconverted;

And I alse tried this, but it ALWAYS returns 0.

stringstream convert(line);
if ( !(convert >> lineconverted) ) {
    lineconverted  = 0;
}

Thanks in advance :)

EDIT: For the first solution I used (gibberish).. Here's a snapshot enter image description here

PewK
  • 397
  • 3
  • 8
  • 19
  • "gibberish numbers and letter", all in a double variable? How? – Mohammad Dehghan May 25 '13 at 08:51
  • What is the contents of `line`? – Mohammad Dehghan May 25 '13 at 08:52
  • A double value "50000" – PewK May 25 '13 at 08:52
  • @MD.Unicorn I posted a pic – PewK May 25 '13 at 08:59
  • possible duplicate of [How can I convert string to double in C++?](http://stackoverflow.com/questions/392981/how-can-i-convert-string-to-double-in-c) – jogojapan May 25 '13 at 09:03
  • Did you ever tried to use *atof* function? Or just decided to use pure stringstream. http://stackoverflow.com/a/4754486/1406063 – Ed Abucay May 25 '13 at 09:03
  • Regarding your code: You realize that `string line` is default-initialized, so it's empty? – jogojapan May 25 '13 at 09:05
  • I tried atof as well, still doesnt return the double value which is 50000, it displays '0', even when I used atof – PewK May 25 '13 at 09:06
  • @jogojapan I havent shown the complete code. It reads from a file line by line. Thus the name, line :) It is not empty – PewK May 25 '13 at 09:06
  • 3
    That "gibberish" in the image is not gibberish, it is a way of representing a very small number: http://en.wikipedia.org/wiki/Scientific_notation#E_notation Of course that is not the number you actually wanted. – BoBTFish May 25 '13 at 09:09
  • Oh I see.. you have trouble reading the scientific notation.... You can use `std::cout << std::fixed << lineconverted` to output it in the usual notation. Make sure you `#include `. – jogojapan May 25 '13 at 09:13
  • Are you sure the string you pass to `istringstream` isn't empty after all? If the extremely small value you get right now isn't what you expected, the cause may very well be that the strings your passing are empty, in which case the `double` won't be changed, and since you don't initialize it, it will have an unpredictable value. – jogojapan May 25 '13 at 09:29
  • Print the value of `line` to the console before converting. Obviously, there is something wrong with contents of `line`. Your code is fine. – Mohammad Dehghan May 25 '13 at 10:30

3 Answers3

13
#include <sstream>

int main(int argc, char *argv[])
{
    double f = 0.0;

    std::stringstream ss;
    std::string s = "3.1415";

    ss << s;
    ss >> f;

    cout << f;
}

The good thing is, that this solution works for others also, like ints, etc.

If you want to repeatedly use the same buffer, you must do ss.clear in between.

There is also a shorter solution available where you can initialize the value to a stringstream and flush it to a double at the same time:

#include <sstream>
int main(int argc, char *argv[]){
   stringstream("3.1415")>>f ;
}
Devolus
  • 21,661
  • 13
  • 66
  • 113
7

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 <sstream>
#include <string>
#include <iostream>

int main()
{
    std::string str;
    std::cin >> str;
    std::istringstream iss(str);
    double d = 0;
    iss >> d;
    std::cout << d << std::endl;
    return 0;
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
0

If you want to store (to a vector for example) all the doubles of a line

#include <iostream>
#include <vector>
#include <iterator>

int main()
{

  std::istream_iterator<double> in(std::cin);
  std::istream_iterator<double> eof;
  std::vector<double> m(in,eof);

  //print
  std::copy(m.begin(),m.end(),std::ostream_iterator<double>(std::cout,"\n"));

}
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133