468,511 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

Converting String to Integer or Double

Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into
Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()

{
string S ; //
S = "990123";
double D = double();

D = atoi( S.c_str());

cout << D << " <--- Interger String -----> " <<S.c_str();
getch();
}

Jul 23 '05 #1
4 3499
Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into

Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()
{
string S ; //
S = "990123";
double D = double();
D = atoi( S.c_str());
cout << D << " <--- Interger String -----> "
<<S.c_str();
getch();
}

Jul 23 '05 #2
ra***********@yahoo.com wrote:
Its very simple in VC++. In the followeing code I have declared a
String, and a double than I am taking the string and converting it into
Double. getch() at the end is only to pause the screen so you can see
the result.
#include <conio.h>
#include <iostream>
using namespace std;
void main ()

{
string S ; //
S = "990123";
double D = double();

D = atoi( S.c_str());

cout << D << " <--- Interger String -----> " <<S.c_str();
getch();
}

try to use stringstreams . they are much more type-safe.
Jul 23 '05 #3
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.

Jul 23 '05 #4
Sorry I guess when I typed I some how missed my question. My original
question was is there a better way to do what I am doing. And second I
do agree that declaring and initializing variables like this reduces
readability, but I think it makes more sense that a variable should be
assigned when it is created just to be on the safe side. Then I know
that my variable contains no garbage.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Asbjørn Ulsberg | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
2 posts views Thread by Alex Buell | last post: by
116 posts views Thread by Dilip | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.