about atoi

Discussion in 'C++' started by sam, Jan 23, 2007.

  1. sam

    sam Guest

    Hi, whats the meaning of atoi function here.


    int atoi(char __ch)
    {
    switch(__ch)
    {
    case '0':return 0;
    case '1':return 1;
    case '2':return 2;
    case '3':return 3;
    case '4':return 4;
    case '5':return 5;
    case '6':return 6;
    case '7':return 7;
    case '8':return 8;
    case '9':return 9;
    };
    return -1;
    }
    sam, Jan 23, 2007
    #1
    1. Advertising

  2. sam wrote:
    > Hi, whats the meaning of atoi function here.


    The meaning is the following:

    > int atoi(char __ch)
    > {
    > switch(__ch)
    > {
    > case '0':return 0;
    > case '1':return 1;
    > case '2':return 2;
    > case '3':return 3;
    > case '4':return 4;
    > case '5':return 5;
    > case '6':return 6;
    > case '7':return 7;
    > case '8':return 8;
    > case '9':return 9;
    > };
    > return -1;
    > }


    (Can't you figure it out yourself?)

    Regards,
    Daniel
    Daniel Albuschat, Jan 23, 2007
    #2
    1. Advertising

  3. On 2007-01-23 18:04, sam wrote:
    > Hi, whats the meaning of atoi function here.


    atoi is short for ascii to integer, it converts a char-string like "345"
    to the integer 345. This however is someones attempt to implement that
    function on their own and this version can only handle one-digit number.
    There are better ways to do that, something like this (haven't tried):

    int ator(char ch)
    {
    if (ch < '0' || ch > '9')
    return -1;
    else
    return ch - '0';
    }

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jan 23, 2007
    #3
  4. sam

    Default User Guest

    sam wrote:

    > Hi, whats the meaning of atoi function here.
    >
    >
    > int atoi(char __ch)


    atoi() is a standard library function. It does not have the signature
    given above. It should be:

    int atoi(const char *nptr);

    We'll assume it's some other function name and go from there.

    > {
    > switch(__ch)


    Identifiers starting with __ are reserved for the implemenation.

    > {
    > case '0':return 0;
    > case '1':return 1;
    > case '2':return 2;
    > case '3':return 3;
    > case '4':return 4;
    > case '5':return 5;
    > case '6':return 6;
    > case '7':return 7;
    > case '8':return 8;
    > case '9':return 9;
    > };


    This switch is rather pointless. You can do the same thing more simply
    with:

    if (ch < '0' || ch > '9')
    return -1;
    else
    return ch - '0';



    Brian
    Default User, Jan 24, 2007
    #4
  5. sam

    Guest

    On Jan 23, 10:04 pm, "sam" <> wrote:
    > Hi, whats the meaning of atoi function here.
    >
    > int atoi(char __ch)
    > {
    > switch(__ch)
    > {
    > case '0':return 0;
    > case '1':return 1;
    > case '2':return 2;
    > case '3':return 3;
    > case '4':return 4;
    > case '5':return 5;
    > case '6':return 6;
    > case '7':return 7;
    > case '8':return 8;
    > case '9':return 9;
    > };
    > return -1;
    >

    YOUR /Above written atoi takes one input character and return
    appropriate integer or -1 as error.

    1. Give it Good Name so it will not conflict.
    2. Dont Provide input string, it is asking for Input char.
    3. I think __varname is platform reserved variable, but i am not sure.
    4. Is there need of this function cant you use atoi (widely used) ?
    5. Why I am answering to this question ?

    >
    >
    > }- Hide quoted text -- Show quoted text -
    , Jan 25, 2007
    #5
    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. lonelyplanet999
    Replies:
    8
    Views:
    42,912
  2. Magig Boatman
    Replies:
    1
    Views:
    460
    Russell Hanneken
    Jul 11, 2003
  3. Mike Chirico
    Replies:
    2
    Views:
    3,818
    Grumble
    Nov 19, 2003
  4. Christopher Benson-Manica

    atoi()

    Christopher Benson-Manica, Jan 15, 2004, in forum: C++
    Replies:
    16
    Views:
    11,161
    Jeff Schwab
    Jan 18, 2004
  5. Neil Vice

    Re: atoi - please help

    Neil Vice, Apr 1, 2004, in forum: C++
    Replies:
    0
    Views:
    352
    Neil Vice
    Apr 1, 2004
Loading...

Share This Page