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

1. ### samGuest

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

2. ### Daniel AlbuschatGuest

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

3. ### =?ISO-8859-1?Q?Erik_Wikstr=F6m?=Guest

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
4. ### Default UserGuest

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
5. ### 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