0

To start off, I have thoroughly examined other questions on that matter and nothing helped me. I need to convert a string into a long double type, and nothing works for me.

string a="634.232";
long double x;
x=strtold(a.c_str(),0);

For example: this "strtold" changes this input into this output.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
YogoWafel
  • 101
  • 2
  • 9

2 Answers2

4

Check for errors:

#include <cerrno>
#include <cstdlib>
#include <iostream>

const char str[] = "634.232";

int main()
{
    char * e;
    errno = 0;
    long double val = std::strtold(str, &e);

    if (*e != '\0' || errno != 0) { /* error */ std::abort(); }

    std::cout << "SUccessfully parsed: " << val;
}

Note that string parsing can fail in multiple ways: The string may not, or not in its entirety, represent a number, or the number that it does represent may be too large to fit into the data type. You have to check for all those possibilities, which is what this code is doing. The end pointer e checks that we've consumed the entire string, and the errno checks that the conversion succeeded.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • [Demo](http://ideone.com/cTgbzO), [Demo with `std::string`](http://ideone.com/4cD5kF). – Kerrek SB Oct 21 '14 at 23:47
  • But I need to convert a string, typically string, not an array of chars. It makes difference in my project. – YogoWafel Oct 21 '14 at 23:58
  • 2
    @NWafel: Isn't that like complaining that you want to see a movie, not images played in a sequence with a soundtrack? Did you see the second demo? You can get a view of the underlying array from an `std::string`. – Kerrek SB Oct 22 '14 at 00:00
2

strtold should work fine, as KerrekSB suggests.

But while you're checking your work I'd recommend using stold: http://www.cplusplus.com/reference/string/stold/

string a{"634.232"};
long double x{stold(a)};
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    Might want to wrap the call to `stold` in a `try` - `catch` block. – user657267 Oct 21 '14 at 23:58
  • @user657267 That's a great call actually, error handling isn't available in C, so that's another benefit that you'll get from using `stold`. – Jonathan Mee Oct 22 '14 at 00:04
  • @JonathanMee: "error handling isn't available in C" is a statement I would regard with a grain of skepticism (especially given that I just described in detail how to handle all kinds of specific errors through the C interface). – Kerrek SB Oct 22 '14 at 00:06
  • How to use this in for? long double x[15]; string a[15]; for (;;) { ? } – YogoWafel Oct 22 '14 at 00:07
  • @NWafel so looking at your code you first need to initialize all 15 `std::string`s. Then you could do something like: `transform(begin(c),end(c),begin(a),bind(static_cast(&stold),placeholders::_1,nullptr));` Shown here: http://ideone.com/WxZtV8 – Jonathan Mee Oct 22 '14 at 01:01
  • @KerrekSB I guess I should qualify my error handling statement to say `stold` handles errors the C++ way, using `try`-`catch` right out of the box. Using `strtold` you'll need to go the C route of checking `errno`, and then throwing your own error if you want to use `try`-`catch`. – Jonathan Mee Oct 22 '14 at 10:23