Search code examples
c++atoi

atoi conversion to return a single digit value


I have a character array like this :

+---+---+---+
|53.|.7.|...|
|6..|195|...|
|.98|...|.6.|
+---+---+---+

I am using an int array to store particular values at specific indexes. For conversion i have used

            for(int i=0;i<27;i++)
        {
        inputNumArray[i]=atoi(&inputInitial[indexArray[i]]);        
        }

now the problem is my desired out put is :

5       3      0       0       7       0       0       0       0
6       0      0       1       9       5       0       0       0
0       9      8       0       0       0       0       6       0

and the code returns me this :

53      3       0       0       7       0       0       0       0
6       0       0       195     95      5       0       0       0
0       98      8       0       0       0       0       6       0

I assume the reason is that atoi scans till it finds character and for atoi(&inputInitial[i]) it will read till i+1, i+2... and so on till it encounters an error. I want to restrict the atoi scanning to a single character only. Is it possible or shall i use some other function ?


Solution

  • Don't use atoi, just use this:

    if(isdigit(c))
        val = c - '0';
    else
        val = 0