>>is this the right way?
No because atoi() takes a pointer to a null-terminated string and your variable temp is just a single character.
The quickest way is to simply subtract the ascii value of '0' from it like this:
int n = temp - '0';. Why does that work? Its because a char is a small integer and if you look at an
ascii chart you will see that all characters have an ascii numerical value. Internally, programs know nothing at all about characters, only numeric values as shown in that ascii chart.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Offline 21,963 posts
since Aug 2005