Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atoi conversion to return a single digit value

Tags:

c++

atoi

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 ?

like image 578
typedefcoder2 Avatar asked Apr 15 '26 01:04

typedefcoder2


1 Answers

Don't use atoi, just use this:

if(isdigit(c))
    val = c - '0';
else
    val = 0
like image 101
zmbq Avatar answered Apr 17 '26 13:04

zmbq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!