Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String to int and double conversion?

Reply
Thread Tools

String to int and double conversion?

 
 
el_boricua
Guest
Posts: n/a
 
      08-01-2005
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

 
Reply With Quote
 
 
 
 
Gang Ji
Guest
Posts: n/a
 
      08-01-2005
atoi and atof in <cstdlib>

el_boricua wrote:
> 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
>

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      08-01-2005
el_boricua wrote:
> 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:stringstream and std::istringstream.


Jonathan

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-02-2005
On Mon, 01 Aug 2005 12:19:09 -0700, Gang Ji <(E-Mail Removed)>
wrote in comp.lang.c++:

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.

> el_boricua wrote:
> > 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


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
elviin
Guest
Posts: n/a
 
      08-02-2005
template<typename T>
T convertTo(const string& fromString) {
T toT;
istringstream(fromString)>>toT;
return toT;
}

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-02-2005
elviin wrote:
> 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.
 
Reply With Quote
 
Xie Yubo
Guest
Posts: n/a
 
      08-02-2005
Jacob wrote:
> elviin wrote:
>
>> 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.

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: http://www.velocityreviews.com/forums/(E-Mail Removed) Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
 
Reply With Quote
 
elviin
Guest
Posts: n/a
 
      08-02-2005
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:ut_of_range( "convertToStdNumericType is
std: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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
char int string ,the different? and string 2 double convent problem. key9 C++ 2 11-29-2006 05:40 PM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments