Re: Problems converting a substring to double

Discussion in 'C++' started by Chris \( Val \), Jul 18, 2003.

  1. "Daniel Wetzler" <> wrote in message
    news:bf8mo8$bsgr5$...
    | Hallo,
    |
    | I'm desperately searching for help in the following case :
    |
    | I import a string-variable calles "zeile" into my routine.
    | This Variable hods a line extracted from a file which loos like this :
    |
    | ATOM 1 N LEU A 1 46.582 41.671 38.773 1.00 25.95
    | 1OVO 117
    |
    | I would like to extract the float values liek e.g. 46.852 as float
    | or double.
    |
    |
    | int protein::lese_pdb_atom (string zeile) {
    |
    | string temp;
    | string bezeichnung;
    | float xkoor;
    |
    | bezeichnung = zeile.substr(18,3);
    | // this works
    |
    |
    | temp = zeile.substr(31,2);
    | xkoor = atof(temp);
    | // this doesn't work

    'atof' requires a const char*
    Use 'temp.c_str()' instead.

    [snip more of the same].

    Better to use 'stringstream(s)' or 'std::strto*()'
    family of functions. They are much safer to use than
    the 'ato*()' functions.

    Here is a quick example:

    // Sample string...
    std::string Line( "ATOM 46.582 41.671" );

    // With a newly created string...
    std::string N( Line, 6, 6 );
    double A( std::strtod( N.c_str(), 0 ) );
    std::cout << A * 2 << std::endl;

    // Or use a temporary...
    double B( std::strtod(
    std::string( Line, 6, 6 ).c_str(), 0 ) );
    std::cout << B * 2 << std::endl;

    You will need to include: '<cstdlib>' to use 'std::strtod()'.

    Consult your documentation for 'std::strtod()'(and others),
    and how the last parameter can help with error trapping.

    Cheers.
    Chris Val
    Chris \( Val \), Jul 18, 2003
    #1
    1. Advertising

  2. Thank you I must admit that I only knw ato*() functions.

    Greetings Daniel



    Chris ( Val ) wrote:

    > Here is a quick example:
    >
    > // Sample string...
    > std::string Line( "ATOM 46.582 41.671" );
    >
    > // With a newly created string...
    > std::string N( Line, 6, 6 );
    > double A( std::strtod( N.c_str(), 0 ) );
    > std::cout << A * 2 << std::endl;
    >
    > // Or use a temporary...
    > double B( std::strtod(
    > std::string( Line, 6, 6 ).c_str(), 0 ) );
    > std::cout << B * 2 << std::endl;
    >


    >
    Daniel Wetzler, Jul 18, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Vecerina
    Replies:
    2
    Views:
    539
    Ivan Vecerina
    Jul 18, 2003
  2. Daniel Wetzler
    Replies:
    0
    Views:
    360
    Daniel Wetzler
    Jul 18, 2003
  3. Sydex
    Replies:
    12
    Views:
    6,454
    Victor Bazarov
    Feb 17, 2005
  4. sparkydarky

    Converting a substring to Integer

    sparkydarky, Feb 5, 2008, in forum: C++
    Replies:
    26
    Views:
    807
    James Kanze
    Feb 9, 2008
  5. Replies:
    3
    Views:
    190
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page