0

Possible Duplicate:
How to convert a 2d char array to a 2d int array?

I'm trying to read input from stdin and stop reading when encountering EOF. I need to store these values as integers in a 2x2 array, array[i][j].

I'm reading in 81 Sudoku characters (integers) from a single line + 2 more characters (\n and EOF) for a total of 83.

Ex:

STDIN -- > 123414292142341......2\n <EOF>

How do I only store the numbers in each array[i][j]? and stop reading in when I encounter an <EOF> ?

So, after every 9 ints, I need to increment j to get the next row.

I'm looking to do this in C++

Thanks!


I have tried this so far

//Read in single line of 83 characters (81 sudoku integers + \n and //Store each integer into array of corresponding row and column

#include iostream

using namespace std;

string input_line;

int main()
{

  while(cin) {
    getline(cin, input_line);
  };


  return 0;
}

How do I tokenize the string "input_line" into a character of integers? and then to atoi to convert to int, and then finally store them in the Sudoku array??


OK, thanks almost done. but I now keep getting an invalid conversion from 'char' to 'const char*' error!

#include <iostream>
#include <stdlib.h>

using namespace std;

string input_line;

int main()
{

  char buffer[9][9];
  int sudoku[9][9];

  int v;

  cout << "Enter input: " << endl;

  cin >> (char*)buffer;

  for(int i = 0; i < 9; i++)
        for(int j = 0; j < 9; j++)
                v = atoi(buffer[i][j]);
                sudoku[i][j] = v;
Community
  • 1
  • 1
Tom
  • 59
  • 2
  • 6
  • 1
    Is this homework? What have you tried? What don't you understand? – GWW Jul 05 '11 at 03:40
  • 1
    One of your classmates already asked about this yesterday: [How to convert a 2d char array to a 2d int array?](http://stackoverflow.com/questions/6566856/how-to-convert-a-2d-char-array-to-a-2d-int-array) He also made a lot more progress before asking for help. – Ben Voigt Jul 05 '11 at 03:52
  • use cin.get() for extracting characters and storing as int....form a loop-in-a-loop structure to iterate through array ondices and above all try a bit harder insolving the problem cause its not the hardest problem out there... – jemmanuel Jul 05 '11 at 03:54
  • @Tom: Your code is a mess. You have a buffer overflow reading from `cin`. Your attempt to assign into `sudoku[i][j]` is outside your loop, with the result that you try to access `sudoku[9][9]`, which doesn't exist (if it even compiles, which it won't with the new for scope rules). You're trying to pass a character to `atoi` which requires a string. Go study my answer to Phil's question. – Ben Voigt Jul 05 '11 at 04:07

1 Answers1

-1

If you want in C++ then you may choose to use C++ style cin. Following is pseudo code:

cout<<"Enter input:\n";
char sudoku[9][9];
cin>>&sudoku[0][0];

Here we are taking advantage of the fact that any digit will be less than < 256. So your table will be arranged inside the sudoku[][] automatically.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • That cast isn't valid. Did you mean `&sudoku[0][0]`? – Ben Voigt Jul 05 '11 at 03:53
  • @Ben, this is valid case, I have checked in the g++ compiler. I am trying to leverage the fact that 2D array will be layed out as 1D array. Means `a[9][9]` is equivalent to `a[81]` in memory layout. – iammilind Jul 05 '11 at 03:54
  • 1
    @iammlind: Regardless of what g++ does, this is undefined behavior. – Ben Voigt Jul 05 '11 at 04:03
  • Who upvoted a buffer overflow? – Ben Voigt Jul 05 '11 at 04:04
  • @Ben, I have changed it. And there is **no buffer overflow**. If the user is **entering 81 characters** and pressing *enter* key. OP has clearly mentioned to enter 81 character. (though he assumes 2 more character which is not needed). Note that This will be used as 2D character array and not as a character buffer. – iammilind Jul 05 '11 at 04:06
  • 1
    Where does the terminating NUL go, exactly? – Ben Voigt Jul 05 '11 at 04:08