Converting String to Integer or Double

R

rainmaker1234

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();
}
 
S

Shani

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();


}
 
R

Rolf Magnus

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.

And what's your question about it?
 
K

Karthik Kumar

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.
 
E

evaned

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.
 
S

Shani

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top