string to int

Discussion in 'C Programming' started by shan, Dec 10, 2005.

  1. shan

    shan Guest

    Hi to all,
    Is there any way to convert string to integer.string will
    contain only integers.I am using turbo c++
    example :
    "321" to 321
    shan, Dec 10, 2005
    #1
    1. Advertising

  2. "shan" <> writes:

    >Hi to all,
    > Is there any way to convert string to integer.string will
    >contain only integers.I am using turbo c++
    >example :
    > "321" to 321



    int i = atoi("321");

    --
    Chris.
    Chris McDonald, Dec 10, 2005
    #2
    1. Advertising

  3. Chris McDonald wrote:
    > "shan" <> writes:


    > > Is there any way to convert string to integer.string will
    > >contain only integers.I am using turbo c++
    > >example :
    > > "321" to 321

    >
    > int i = atoi("321");


    strtol() would be better, atoi() has poor error handling


    --
    Nick Keighley
    Nick Keighley, Dec 10, 2005
    #3
  4. shan

    pete Guest

    Chris McDonald wrote:
    >
    > "shan" <> writes:
    >
    > >Hi to all,
    > > Is there any way to convert string to integer.string will
    > >contain only integers.I am using turbo c++
    > >example :
    > > "321" to 321


    #include <stdlib.h>

    > int i = atoi("321");


    --
    pete
    pete, Dec 10, 2005
    #4
  5. shan

    John Bode Guest

    shan wrote:
    > Hi to all,
    > Is there any way to convert string to integer.string will
    > contain only integers.I am using turbo c++
    > example :
    > "321" to 321


    #include <stdlib.h>
    #include <ctype.h>
    ....
    char *chk;
    int val = (int) strtol("321", &chk, 10);
    if (!isspace((int) *chk) && *chk != 0)
    {
    /* input string contains invalid character */
    }
    John Bode, Dec 10, 2005
    #5
  6. John Bode wrote:
    > shan wrote:
    > > Hi to all,
    > > Is there any way to convert string to integer.string will
    > > contain only integers.I am using turbo c++
    > > example :
    > > "321" to 321

    >
    > #include <stdlib.h>
    > #include <ctype.h>
    > ...
    > char *chk;
    > int val = (int) strtol("321", &chk, 10);


    The cast produces implementation-defined behavior if the return value
    of strtol exceeds the representable range of int. Jack Klein has
    written a very robust example of how to do this correctly at
    http://home.att.net/~jackklein/c/code/strtol.html.

    Robert Gamble
    Robert Gamble, Dec 10, 2005
    #6
  7. shan

    Stan Milam Guest

    shan wrote:
    > Hi to all,
    > Is there any way to convert string to integer.string will
    > contain only integers.I am using turbo c++
    > example :
    > "321" to 321
    >


    atoi(), or strtod(), or even sscanf().
    Stan Milam, Dec 11, 2005
    #7
  8. shan

    Joe Wright Guest

    Stan Milam wrote:
    > shan wrote:
    >
    >> Hi to all,
    >> Is there any way to convert string to integer.string will
    >> contain only integers.I am using turbo c++
    >> example :
    >> "321" to 321
    >>

    >
    > atoi(), or strtod(), or even sscanf().


    That would be strtol(), surely.
    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Dec 11, 2005
    #8
  9. shan

    SM Ryan Guest

    "shan" <> wrote:
    # Hi to all,
    # Is there any way to convert string to integer.string will
    # contain only integers.I am using turbo c++
    # example :
    # "321" to 321

    strtol and related routines, or sscanf.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    The little stoner's got a point.
    SM Ryan, Dec 12, 2005
    #9
    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. Schnoffos
    Replies:
    2
    Views:
    1,196
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,613
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    431
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    932
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    417
    James Kanze
    Apr 17, 2007
Loading...

Share This Page