Converting a String to an Integer

Discussion in 'C++' started by sakitah, Oct 5, 2005.

  1. sakitah

    sakitah Guest

    Hello Everyone,

    Here's the problem: I have a string that I break down into 3 other
    strings as such:

    string1 = "001-202-1234567"

    and parsing it into:

    string2 = "001";
    string3 = "202";
    string4 = "1234567";


    I want to manipulate these as numbers. I used the append function for
    strings to fill the new strings from the original string. Sadly, I
    cant append from the original string into an integer, so how can I do
    it?


    Thank You
    Sakitah


    Reply
     
    sakitah, Oct 5, 2005
    #1
    1. Advertising

  2. sakitah wrote:
    > Here's the problem: I have a string that I break down into 3 other
    > strings as such:
    >
    > string1 = "001-202-1234567"
    >
    > and parsing it into:
    >
    > string2 = "001";
    > string3 = "202";
    > string4 = "1234567";
    >
    >
    > I want to manipulate these as numbers. I used the append function for
    > strings to fill the new strings from the original string. Sadly, I
    > cant append from the original string into an integer, so how can I do
    > it?


    Search Google Groups for 'conversion int to string c++'. And please
    do search Google for answers before posting. Otherwise you're wasting
    your precious time waiting for an answer here.

    V
     
    Victor Bazarov, Oct 5, 2005
    #2
    1. Advertising

  3. sakitah

    mlimber Guest

    sakitah wrote:
    > Hello Everyone,
    >
    > Here's the problem: I have a string that I break down into 3 other
    > strings as such:
    >
    > string1 = "001-202-1234567"
    >
    > and parsing it into:
    >
    > string2 = "001";
    > string3 = "202";
    > string4 = "1234567";
    >
    >
    > I want to manipulate these as numbers. I used the append function for
    > strings to fill the new strings from the original string. Sadly, I
    > cant append from the original string into an integer, so how can I do
    > it?
    >
    >
    > Thank You
    > Sakitah
    >
    >
    > Reply


    Please consult this FAQ:

    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.2

    Cheers! --M
     
    mlimber, Oct 5, 2005
    #3
  4. sakitah

    jalkadir Guest

    I hope this helps you.
    As a word of advise, stay with the C++ style, but I am giving you the
    two methodologies.

    This is just a snip of a class I wrote to handle this kind of problem,
    if you want the full class, just write to me directly using the same
    subject title and I will send you a copy, OK?
    --snip
    const int jme::strtools::toInt( const std::string& s ) throw (
    jme::strtoolsEx ) {
    //Test the string
    if ( s.empty() )
    {
    throw jme::strtoolsEx( str_empty, FILE, METHOD, LINE );
    }
    // This is what you want <<==
    std::istringstream iss( s );
    int num;
    //If the data extracted from 'iss' is successfully assingned to
    // num and iss retuns false
    if ( iss >> num && iss.eof() )
    return num;
    else
    throw jme::strtoolsEx( fe_eof_bit, FILE, METHOD, LINE ); <<==
    this is my own exception, but it gives you an idea as to how to hadle
    the problem.

    /*
    // The simple 'C' version
    if(s.empty()) throw jme::strtoolsEx(STR_EMPTY,FILE,METHOD,LINE);
    return atoi(s.c_str());
    */
    }
     
    jalkadir, Oct 5, 2005
    #4
  5. sakitah

    Mark P Guest

    sakitah wrote:
    > Hello Everyone,
    >
    > Here's the problem: I have a string that I break down into 3 other
    > strings as such:
    >
    > string1 = "001-202-1234567"
    >
    > and parsing it into:
    >
    > string2 = "001";
    > string3 = "202";
    > string4 = "1234567";
    >
    >
    > I want to manipulate these as numbers. I used the append function for
    > strings to fill the new strings from the original string. Sadly, I
    > cant append from the original string into an integer, so how can I do
    > it?
    >
    >
    > Thank You
    > Sakitah
    >
    >
    > Reply
    >


    On top of what's been mentioned you may also want to look up strtol
     
    Mark P, Oct 6, 2005
    #5
  6. sakitah

    Daniel Moree Guest

    sakitah wrote:
    > Hello Everyone,
    >
    > Here's the problem: I have a string that I break down into 3 other
    > strings as such:
    >
    > string1 = "001-202-1234567"
    >
    > and parsing it into:
    >
    > string2 = "001";
    > string3 = "202";
    > string4 = "1234567";
    >
    >
    > I want to manipulate these as numbers. I used the append function for
    > strings to fill the new strings from the original string. Sadly, I
    > cant append from the original string into an integer, so how can I do
    > it?
    >
    >
    > Thank You
    > Sakitah
    >
    >
    > Reply
    >
    > From - Wed


    Depending on what type of string you are using, you will need to use 2
    types of functions.

    if the string is
    std:string
    then use atoi()
    if it is LPCTSTR then use
    strtol

    both are in the MSDN!

    Daniel moree
     
    Daniel Moree, Oct 6, 2005
    #6
  7. Daniel Moree wrote:
    >
    > if the string is
    > std:string
    > then use atoi()
    > if it is LPCTSTR then use
    > strtol


    Why atoi() with std::string ?

    Never recommend atoi(). It has a serious design flaw:
    You cannot figure out if the conversion worked or produced
    an error. atoi() is in the same category as gets(). Never, ever
    recommend those functions. It would have been better if those
    functions never were invented.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 6, 2005
    #7
  8. sakitah

    Ron Natalie Guest

    Karl Heinz Buchegger wrote:
    > Daniel Moree wrote:
    >> if the string is
    >> std:string
    >> then use atoi()
    >> if it is LPCTSTR then use
    >> strtol

    >
    > Why atoi() with std::string ?
    >
    > Never recommend atoi(). It has a serious design flaw:
    > You cannot figure out if the conversion worked or produced
    > an error. atoi() is in the same category as gets(). Never, ever
    > recommend those functions. It would have been better if those
    > functions never were invented.
    >

    Further, in the case of an overflow, the behavior is undefined.
    That's even worse than just not being able to differentiate between
    0 and an error return.
     
    Ron Natalie, Oct 6, 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. claire.bell1

    Converting an integer to a string.

    claire.bell1, Jul 19, 2003, in forum: C++
    Replies:
    0
    Views:
    358
    claire.bell1
    Jul 19, 2003
  2. Gil
    Replies:
    13
    Views:
    1,078
    Default User
    Jan 29, 2004
  3. Alex Neumann

    Converting a string to an integer

    Alex Neumann, May 10, 2004, in forum: C++
    Replies:
    6
    Views:
    3,236
    Leor Zolman
    May 11, 2004
  4. Replies:
    5
    Views:
    5,648
    Shani
    Mar 19, 2005
  5. Randy Kramer
    Replies:
    12
    Views:
    408
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page