String to int and double conversion?

Discussion in 'C++' started by el_boricua, Aug 1, 2005.

  1. el_boricua

    el_boricua Guest

    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
     
    el_boricua, Aug 1, 2005
    #1
    1. Advertising

  2. el_boricua

    Gang Ji Guest

    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
    >
     
    Gang Ji, Aug 1, 2005
    #2
    1. Advertising

  3. 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::eek:stringstream and std::istringstream.


    Jonathan
     
    Jonathan Mcdougall, Aug 1, 2005
    #3
  4. el_boricua

    Jack Klein Guest

    On Mon, 01 Aug 2005 12:19:09 -0700, Gang Ji <>
    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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 2, 2005
    #4
  5. el_boricua

    elviin Guest

    template<typename T>
    T convertTo(const string& fromString) {
    T toT;
    istringstream(fromString)>>toT;
    return toT;
    }
     
    elviin, Aug 2, 2005
    #5
  6. el_boricua

    Jacob Guest

    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.
     
    Jacob, Aug 2, 2005
    #6
  7. el_boricua

    Xie Yubo Guest

    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: Website: http://xieyubo.cn/
    Harbin Institute of Technology
    Phone: 86-451-86416614 Fax: 86-451-86413309
     
    Xie Yubo, Aug 2, 2005
    #7
  8. el_boricua

    elviin Guest

    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.
     
    elviin, Aug 2, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kaede
    Replies:
    1
    Views:
    503
    David Hilsee
    Sep 4, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,601
    Victor Bazarov
    Feb 17, 2005
  3. Schnoffos
    Replies:
    2
    Views:
    1,247
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,697
    Old Wolf
    Jan 20, 2004
  5. Shriramana Sharma
    Replies:
    8
    Views:
    290
    Gerhard Fiedler
    Jun 18, 2013
Loading...

Share This Page