424,987 Members | 997 Online
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

# converting strings to double: could someone help me?

 P: n/a 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! Oct 21 '05 #1
5 Replies

 P: n/a #include Oct 21 '05 #2

 P: n/a shang wrote: 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 #include #include int main() { std::string s = "15.4"; double d; std::istringstream iss(s); iss >> d; std::cout << "value of d = " << d << '\n'; return 0; } -- Marcus Kwok Oct 21 '05 #3

 P: n/a On Fri, 21 Oct 2005 18:40:08 +0000, Marcus Kwok wrote: 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 #include #include 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 Oct 21 '05 #4

 P: n/a On 2005-10-21, shang wrote: 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 , with the function atof. If you're already using and in the program you might as well go whole-hog and use stringstream from instead. #include #include #include 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; } -- Neil Cerutti Oct 21 '05 #5

 P: n/a > On Fri, 21 Oct 2005 18:40:08 +0000, Marcus Kwok wrote: 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 #include #include int main() { std::string s = "15.4"; double d; std::istringstream iss(s); iss >> d; std::cout << "value of d = " << d << '\n'; return 0; } Jay Nabonne wrote: 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 #include #include 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; } -- Marcus Kwok Oct 21 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.