This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atoi problems

I am having problems getting atoi to work.

here is how I populate the comin[] array

do
        {
        comin[index] = (getchar());
        ++index;
        }
        while (index<=4);

later in the program I do the following:-

command = atoi(comin);

expecting to get a 4 digit number in command. comin is working fine - no problems. But command is always 0 (zero). I have tried quite a few things, but no luck. I did a search on the forum andd came up with this

http://www.keil.com/forum/docs/thread10470.asp

Anybody else had this problem or know a work around?

Parents
  • Another point here is how do you know that the value is always 0?

    If you are printing it out then remember it is an "int" value returned by ATOI and you need to use the correct print format as it is possible to get silly print values with other formats.

    As for the discussion about when ATOI stops, it is clear in the function description that it stops as soon as it encounters a non numeric character (I guess it applies ISDIGIT or something similar). This means a \n should work although is not really preferable over a \0 as described earlier as this is a proper C string terminator.

    I use ATOI quite a lot on an 89C51 and it works fine for me.

    Cheers,

    Dirk

Reply
  • Another point here is how do you know that the value is always 0?

    If you are printing it out then remember it is an "int" value returned by ATOI and you need to use the correct print format as it is possible to get silly print values with other formats.

    As for the discussion about when ATOI stops, it is clear in the function description that it stops as soon as it encounters a non numeric character (I guess it applies ISDIGIT or something similar). This means a \n should work although is not really preferable over a \0 as described earlier as this is a proper C string terminator.

    I use ATOI quite a lot on an 89C51 and it works fine for me.

    Cheers,

    Dirk

Children