Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Problems converting a substring to double

Reply
Thread Tools

Re: Problems converting a substring to double

 
 
Chris \( Val \)
Guest
Posts: n/a
 
      07-18-2003

"Daniel Wetzler" <(E-Mail Removed)> wrote in message
news:bf8mo8$bsgr5$(E-Mail Removed)...
| 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


 
Reply With Quote
 
 
 
 
Daniel Wetzler
Guest
Posts: n/a
 
      07-18-2003
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;
>


>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a substring to Integer sparkydarky C++ 26 02-09-2008 10:59 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Re: Problems converting a substring to double Ivan Vecerina C++ 2 07-18-2003 06:11 PM
Re: Problems converting a substring to double Daniel Wetzler C++ 0 07-18-2003 12:21 PM



Advertisments