Sudoku problem: Read integers from file without spaces

So i have a file which is 9x9 input data:

036518090
000302000
050967140
000074086
080009002
701000059
640000801
500700020
270490000

(No spaces!!!)

i cant seem to read the data and assign individual int to an array.
Can i read each digit as a char first, find its position then change it back to an integer?

Can i ask for some help please.
Thanks in advance.
Read in each line as a string using getline(), go though each string and convert each char in the string to an int by doing int k = string[i] - '0', then assign k to the array.
I am following the c++ primer, I haven't got to reading from file yet, but this should be your solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>


using namespace std;

int main()
{

    string a;
    int arr[81];
    int arrcnt =0;//holds current index of array
    while(getline(cin, a) && a != "/0"){//store each line in string
            for(int i = 0; i != a.size()-1;++i){ //cycle thru char of string
                arr[arrcnt] = (a[i]-'0'); //converts from base 16 to int and store in array
                cout << arr[arrcnt] << " ";//testing to make sure passed to array properly
                ++arrcnt;} // up index count
        }
return 0;

}
Last edited on
Topic archived. No new replies allowed.