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 t开发者_开发百科he 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;
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.
精彩评论