The Boost library has a 'lexical_cast' class that will do this with
stringstreams, so you don't have to mess with it.
It's not too difficult to do of course, but it provides a nice wrapper.
So here's the lexical_cast version of the original poster's code:
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
void main ()
{
string S = "990123";
double D = lexical_cast<double>(S);
cout << D << " <--- Interger String -----> " <<S;
}
Additional comments about the original code:
Why are you using conio.h? Okay, I realize that getch() provides a nice
'get any key' function, but you can replace it with something like
cin.ignore and require the user to press enter to get a portable
solution.
Second suggestion is to try to put variable initialization at the same
place as the declaration as I have above.
Especially in the case of something like double D = double(); . What
your code does (assuming no optimization) is to assign 0 to D then
immediately assign the value of atoi(S.c_str()); to D. I realize that
premature microoptimizations like this are usually a bad idea, but that
doesn't mean you should try to create more work. Adding the '=
double()' there has no purpose and, IMO, reduces readability instead of
enhancing it.
Finally, why '<< S.c_str(); '? The C++ streams know about the string
class (or more accurately, the string class knows about the C++
streams), so '<< S' will work fine.