converting strings to double: could someone help me?


S

shang

Hi!
I am trying to find a function that converts a string into a double
or a long double number. I found atod() but I don't know which library
I have to include to use it. Could someone help me?

Thanks!
 
Ad

Advertisements

M

Marcus Kwok

shang said:
Hi!
I am trying to find a function that converts a string into a double
or a long double number. I found atod() but I don't know which library
I have to include to use it. Could someone help me?

You should not use the ato*() functions (atoi, atof, etc.) because you
have no way of determining whether the input was a 0 or something that
caused an error. You should prefer either the strto* functions, or
stringstreams. I like stringstreams:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "15.4";
double d;

std::istringstream iss(s);
iss >> d;

std::cout << "value of d = " << d << '\n';

return 0;
}
 
J

Jay Nabonne

You should not use the ato*() functions (atoi, atof, etc.) because you
have no way of determining whether the input was a 0 or something that
caused an error. You should prefer either the strto* functions, or
stringstreams. I like stringstreams:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "15.4";
double d;

std::istringstream iss(s);
iss >> d;

std::cout << "value of d = " << d << '\n';

return 0;
}

Since you were talking about being able to distinguish an error case from
a value of 0, would you check the state of iss after the ">>" in order to
check for that error state? I assume it would leave "d" unchanged in the
event of error?

- Jay
 
N

Neil Cerutti

Hi!
I am trying to find a function that converts a string into a
double or a long double number.

You're close. You want header <cmath>, with the function atof.

If you're already using <string> and <iostream> in the program
you might as well go whole-hog and use stringstream from
<sstream> instead.

#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
std::string text = "158.2587e-12";
std::stringstream ss;
double number;
ss << text;
ss >> number;
std::cout << text + " * 2.0 = " << number * 2.0 << std::endl;
return 0;
}
 
Ad

Advertisements

M

Marcus Kwok

Jay Nabonne said:
Since you were talking about being able to distinguish an error case from
a value of 0, would you check the state of iss after the ">>" in order to
check for that error state? I assume it would leave "d" unchanged in the
event of error?

Right, sorry, I was in a bit of a hurry when I wrote it. You could do
something like:

if (iss >> d) {
// conversion was successful
}

This program seems to indicate that if the conversion failed, then the
value of 'd' is unchanged:


#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "hello";
double d = 33.0;

std::istringstream iss(s);
if (iss >> d) {
std::cout << "value of d = " << d << '\n';
}
else {
std::cout << "error converting d (d = " << d << ")\n";
}

return 0;
}
 
Ad

Advertisements


Top