String to int and double conversion?

E

el_boricua

What is the best way to convert from a string to a int and from a
string to double? I have a line that is tokenize and I need to parse to
test the tokens and convert them to their respective values from
string.

Thanks,
N
 
J

Jonathan Mcdougall

el_boricua said:
What is the best way to convert from a string to a int and from a
string to double? I have a line that is tokenize and I need to parse to
test the tokens and convert them to their respective values from
string.

Look up std::eek:stringstream and std::istringstream.


Jonathan
 
J

Jack Klein

1. Don't top post. Material in your reply should come AFTER quoted
material from the original post you refer to.
atoi and atof in <cstdlib>

2. Don't post what you don't know. The ato* functions from
<stdlib.h> or <cstdlib> should NEVER be used in a C++ program, or a C
one for that matter. They produce undefined behavior if the converted
value is too large for the return type.

The strto* functions were added to C when it was standardized more
than 15 years ago, specifically to replace the old, unsafe functions,
because the new ones have fully defined behavior with any input other
than a null pointer.

And of course C++ has other methods of its own, as others have pointer
out.
 
E

elviin

template<typename T>
T convertTo(const string& fromString) {
T toT;
istringstream(fromString)>>toT;
return toT;
}
 
J

Jacob

elviin said:
template<typename T>
T convertTo(const string& fromString) {
T toT;
istringstream(fromString)>>toT;
return toT;
}

This looks neat, but how would you do error checking?

int a = convertTo("42"); // OK
int b = convertTo("1x"); // Error
short c = convertTo("100000"); // Error
unsigned d = convertTo("-1"); // Error
int e = convertTo("1.2"); // Error
double f = convertTo("1x"); // Error

Thanks.
 
X

Xie Yubo

Jacob said:
This looks neat, but how would you do error checking?

int a = convertTo("42"); // OK
int b = convertTo("1x"); // Error
short c = convertTo("100000"); // Error
unsigned d = convertTo("-1"); // Error
int e = convertTo("1.2"); // Error
double f = convertTo("1x"); // Error

Thanks.
Write "convertTo" as follow:

template<typename T>
T convertTo(const string& fromString, T& toT)
{
istringstream(str) >> toT;
return toT;
}

and use it like this:

int a;
convertTo("42", a);

try it~~
--
Best Regards

Xie Yubo
Email: (e-mail address removed) Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
 
E

elviin

template<typename T>
inline T convertToFrom( const std::string& fromString ) {
T toT;
std::istringstream tmStream(fromString);
char c;
if( !(tmStream >> toT) || tmStream.get(c))
throw std::eek:ut_of_range( "convertToStdNumericType is
std::eek:ut_of_range." );
return toT;
}

But I think that >> operator IMHO should return a position of any
left-over character instead of bool value. That would provide more
information.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top