how to directly convert char to numbers?

How to convert a 2d char array to a 2d int array?

  • char puzzle[i][j]; int i,j,count=0; char value[81]; for( i = 0; i < 9; i++){ for( j = 0; j < 9; j++){ cin >> value[count]; puzzle[i][j] = value[count]; count++; }} This what I have so far. I tried using atoi but I needed a char * str. The input is: ..4545.. (numbers and periods) I'm trying to convert the char puzzle[i][j] to an int puzzle[i][j]. The char array currently holds "..4545.." and I want to covert it so it holds just integers "00454500".

  • Answer:

    Engaging psychic debugger... Performing Jedi mind tricks... This is the code you want: int puzzle[9][9]; // changed type int i,j,count=0; char value[81]; for( i = 0; i < 9; i++ ) { for( j = 0; j < 9; j++ ) { cin >> value[count]; puzzle[i][j] = value[count] - '0'; // convert from ASCII digit to integer count++; } }

Phil at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.