## brycematheson Newbie Poster

I'm making a Sudoku game. When reading in from a file, I would like to convert all of my "0" to blank spaces. However, no matter what I do, those "blank spaces" always turn to "32" because, that's the value of a space on the ASCII Table. How do I change this? This is my code:

``````for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 9; row++)
{
fin >> sudokuBoard[row][col];
if (sudokuBoard[row][col] == 0)
{
sudokuBoard[row][col] = (char)' ';
}
}
}``````

## xikkub 4

You can't store data as both integers and characters simultaneously.
If your sudoku board is a character array, use the space character (' ') and print them as such when doing output.
If your sudoku board is an integer array, use 0 for blank spaces and print all others as integers when doing output, omitting 0 during the print process.

``````for (int r=0; r<9; r++) {
for (int c=0 c<9; c++) {
if (sudokuBoard[r][c] == 0)
cout << ' ';
else
cout << sudokuBoard[r][c];
}
}``````

## brycematheson

Okay, that's great! Thank you. But now I have a question.

How do I stick this output into a nice 9x9 grid with dividers and such? It's just one long string of output that I don't know how to style, if that makes sense?

## xikkub 4

I provided a quick implementation below. It's not the most efficient, but it gets the job done. The nice-looking dividers are simply printed every 3 characters (to make 3x3 grids). This is done with the modulo operator (%) which takes the remainder of a number. When x%3 is zero, that means x evenly divides into 3. I had to add a little conditional to prevent the loop from printing extra bars at the end of the loops.

[EDIT] I left out the conditional for the 0's (blank case), but I think you can figure out where to add it. Good luck!

``````for (int r=0; r<9; r++) {
for (int c=0; c<9; c++) {
cout << sudokuBoard[r][c]; //print current character
//dont print on last run (c=8), print a pipe if we've printed 3 columns
if (c != 8 && (c + 1) % 3 == 0)
cout << '|';
}
//dont print on last run, print newline if we've printed 3 rows
if (r != 8 && (r + 1) % 3 == 0)
cout << "\n---+---+---";
cout << endl; //newline
}``````

## brycematheson

Oh my goodness! Xikkub, you are my HERO! You have helped me out SO much! Thank you for everything. :)

I just two tiny little questions now. I'm trying to create numbers at the beginning of each row (from 1 to 9) so that people can find the coordinates easily. I also have a Column header, but that was easy. How would I go about making numbers from 1 to 9 along the left edge? I've tried using a FOR loop, but it just messes things up.

Also, I placed spaces in between each number in the table for readability. However, I don't want a space in between the 3rd, 6th, and 9th number and the "|". How would someone go about doing something like that? Thank you very much!

Here's my code so far:

``````void display(int sudokuBoard[][9])
{
cout << "   A B C D E F G H I" << endl;

for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
if (sudokuBoard[row][col] == 0)
cout << ' ';
else
cout << sudokuBoard[row][col] << " ";

//Don't print on last run (c = 8)
//Print vertical line if we've printed 3 columns
if (col != 8 && (col + 1) % 3 == 0)
cout << '|';
}
//Don't print on last run (c = 8)
//Print newline if we've printed 3 rows
if (row != 8 && (row + 1) % 3 == 0)
cout << "\n-----+-----+-----";
cout << endl;
}
getOption(sudokuBoard);
}``````

See my second post below for a little more explanation on what I mean.

## brycematheson

Here is what my output looks like so far:

``````A B C D E F G H I
7 2 3 |   |1 5 9
6   |3  2 |  8
8   | 1  |  2
-----+-----+-----
7  |6 5 4 | 2
4 |2  7 |3
5  |9 3 1 | 4
-----+-----+-----
5   | 7  |  3
4   |1  3 |  6
9 3 2 |   |7 1 4``````

## xikkub 4

Here's a quick implementation that will probably get what you're looking for. I don't quite have time to explain it right now, but I think you can fiddle with it and make it do what you want.

If you would like to add more vertical spacing, you will simply need to add blank lines with pipes (ex: "xxxx|xxxx|xxxx", where x's represent spaces) before and after printing the horizontal dividers ("----+----+----"). I hope this helps.

``````for (int row = 0; row < 9; row++) {
cout << "  "; //spaces to keep the output from the side of the screen (*1)
for (int col = 0; col < 9; col++) {
cout << ' '; //put the space BEFORE printing the character (*2)
if (sudokuBoard[row][col] == 0)
cout << 'X';
else
cout << sudokuBoard[row][col];

if (col != 8 && (col + 1) % 3 == 0)
cout << " |"; //print a space THEN a pipe (without trailing space because of (*2))
}
if (row != 8 && (row + 1) % 3 == 0)
cout << "\n   ------+-------+------"; //print trailing spaces as in (*1), then dividers
cout << endl;
}``````