2

I'm reading in a sodoku board from a text file. The board is represented by 9 rows of 9 digit numbers, like this:

594632817
123478569
678159234
215346798
346897125
789215346
437561982
851924673
962783451

EDIT

Here are the results when I change the while condition to (input >> char):

Output as chars are read in:

96212486
71931369
48728254
35185947
67350

Output of printArray:

962124867
193136948
728254351
859476735

�$%w��
����QȿȔ
L�`g�Pw
���w�

And here's the output for while (!input.eof()):

�94632817
123478569
678159234
215346798
346897125
789215346
437561982
851924673
962783451

END EDIT

The trouble is, when I place each digit into a multidimensional array, the element at [0][0] appears as a shaded question mark (compiled with g++). The problem only surfaces when I'm printing out the contents of the array, the data as it's read in appears to be fine. For what it's work, this also happens if I cout << board[0][0] from the main function.

Any help would be appreciated!

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int createArray(string filename);
bool checkRows(char board[][9]);
bool checkColumns(char board[][9]);
bool checkBoxes(char board[][9]);
void printArray(char board[][9]);

int main ()
{
    char board [9][9];
    int i = 0;
    int j = 0;
    int count = 0;

    ifstream input("board.txt");

    char ch;

    while (input >> ch)
    {
      //  ch = input.get();
        if (ch != '\n')
        {            

        cout << ch;
        board[i][j] = ch;
        j++;

            if (j % 9 == 0)
            {
                i++;
            }

        }
        if (j > 8)
            j = 0;
        if (i > 8)
            i = 0;

        count++;

        if (count % 10 == 0)
            cout << endl;
    }

    input.close();

    printArray(board);
    cout << checkRows(board) << endl;
    cout << checkColumns(board) << endl;

    return 0;

}


void printArray(char board[][9])
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            cout << board[i][j];
        }
    cout << endl;
    }

    cout << board[0][0] << endl;
    cout << board[0][1] << endl;

}
The_Glidd
  • 75
  • 3
  • 8
  • `the element at [0][0] appears as a shaded question mark` Where it appears? `g++` does not visualizes variables. – Ivan Aksamentov - Drop Feb 22 '14 at 00:39
  • It shows up as a strange looking shaded question mark character when I cout. – The_Glidd Feb 22 '14 at 00:43
  • 1
    What is the hex or decimal value of the strange character? – Thomas Matthews Feb 22 '14 at 00:47
  • You're testing for *EOF* incorrectly. Search stack overflow for "c++ end of file". Try `while (input >> ch)`. – Thomas Matthews Feb 22 '14 at 00:48
  • Actually, the while loop fix did not work. It gave my the correct first element, but the rest of the array went south. – The_Glidd Feb 22 '14 at 01:13
  • *What* while-loop fix? The code posted is still broken. Ad [this is the article](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Thomas was eluding to, in case you found the wrong one. – WhozCraig Feb 22 '14 at 01:47
  • Article read, thanks. I also tried to extract the hex value of board[0][0]. printf("0x%x", board[0][0]); yields: 0xffffffff – The_Glidd Feb 22 '14 at 14:18
  • The code you have currently posted, reads one character in the loop condition, but does not use it. It just reads another character, so that only every second character ends up in your array. Try removing the line `ch = input.get();`. – cmaster - reinstate monica Feb 22 '14 at 14:24
  • Perfect, cmaster. Sorry I missed that. Everything looks good now. Problem is solved, but I'm still unclear on why the eof condition affected only the first element of the array. – The_Glidd Feb 22 '14 at 15:01

1 Answers1

0

By doing this, reading ch two times.

Remove ch = input.get(); and you will read each number correctly.

while (input >> ch)
{
    ch = input.get();
    ...
}

Again, consider changing condition below to make sure correct endl placement

if (count % 10 == 0)
    cout << endl;

to

if (count % 9 == 0)
    cout << endl;
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • This solution was also offered in the comments. Thanks. I'm still wondering why exactly the eof termination condition affected the output. – The_Glidd Feb 24 '14 at 01:44