Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C only
Search
Advanced Search
Forums
BYTES
Product Launch
Updates
Developer Toolkit
Member List
Calendar
Today's Posts
Home
Forum
Topic
C
how to convert string to double?
how to convert string to double?
Collapse
This topic is closed.
X
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
kathy
#1
how to convert string to double?
Feb 8 '06, 11:55 PM
I can not find something like atof() which can convert string to
double.
Can anyone help me?
xuatla
#2
Feb 8 '06, 11:55 PM
Re: how to convert string to double?
kathy wrote:[color=blue]
> I can not find something like atof() which can convert string to
> double.
>
> Can anyone help me?
>[/color]
How about just using atof(s.c_str()) , given s is a string in your case?
X
Comment
Post
Cancel
Thomas Tutone
#3
Feb 9 '06, 12:05 AM
Re: how to convert string to double?
kathy wrote:
[color=blue]
> I can not find something like atof() which can convert string to
> double.
>
> Can anyone help me?[/color]
It's in the FAQ:
Miscellaneous technical issues, C++ FAQ
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
You may want to read the rest of the FAQ too - I bet you'll find it
very helpful.
Best regards,
Tom
Comment
Post
Cancel
dakka
#4
Feb 9 '06, 03:25 AM
Re: how to convert string to double?
kathy wrote:[color=blue]
> I can not find something like atof() which can convert string to
> double.
>
> Can anyone help me?
>[/color]
#include <iostream>
#include <sstream>
using namespace std;
double strtodouble(con st string& what)
{
istringstream instr(what);
double val;
instr >> val;
return val;
}
int main()
{
string dbl("3.1415");
cout << strtodouble(dbl ) << endl;
return 0;
}
--
--dakka
Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
Comment
Post
Cancel
kathy
#5
Feb 9 '06, 08:45 PM
Re: how to convert string to double?
The following failed:
istringstream instr;
double val[2];
instr.str(str1) ;
instr >> val[0];
instr.str(str2) ;
instr >> val[2];
why?
Comment
Post
Cancel
kathy
#6
Feb 9 '06, 08:45 PM
Re: how to convert string to double?
wrong typing:
instr >> val[2];
should be
instr >> val[1];
Comment
Post
Cancel
kathy
#7
Feb 9 '06, 08:45 PM
Re: how to convert string to double?
wrong typing:
instr >> val[2];
should be
instr >> val[1];
Comment
Post
Cancel
mlimber
#8
Feb 9 '06, 08:55 PM
Re: how to convert string to double?
kathy wrote:[color=blue]
> The following failed:
>
> istringstream instr;
> double val[2];
> instr.str(str1) ;
> instr >> val[0];[/color]
Because the stream state is currently set to fail, you need to clear
it:
instr.clear();
[color=blue]
> instr.str(str2) ;
> instr >> val[1]; // Fixed as in your followup
>
> why?[/color]
In the future, you should tell us how it failed. Fortunately, this one
was fairly easy to spot.
Cheers! --M
Comment
Post
Cancel
kathy
#9
Feb 9 '06, 09:15 PM
Re: how to convert string to double?
why it set to fail state?
Comment
Post
Cancel
Previous
template
Next
Working...
OK
OK
Cancel
👍
👎
☕
Comment