Hi. I am writing a code and I need to make the 0's of the code into just spaces, but when I try to do that, 32 comes up instead of a space(the ASCII value of space). An example is here of what I have tried, but to no avail
1 2 3 4 5 6 7 8 9 10
int subFunction(int board[][9];)
for (int i = 0; i < 9; i++)
{
for (int j = 0; int j < 9; j++)
{
if (board[i][j] == 0)
board[i][j] = (char)board[i][j];
board[i][j] = ' ';
}
}
This is what I have tried to do to solve the problem, but I can't seem to figure out any other way to do it. Can anyone help?
I am making a sudoku board, and it reads a file containing integers, and when a zero is declared, it should change into a space. meaning, I need all the other numbers in the array.
This is what a file would look something like this(ignore the fact that this wouldnt be enough numbers for a sudoku board, this is just an example of something I am finding in the files)
009
018
021
Additionally here is some code that casts ints to chars. the numbers1-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main() {
int array[] = {1,2,3,4,5,6,7,8,9,9};
for (int i = 0; i < 9; i ++)
cout << array[i] << " - "; // ints
cout << endl;
for (int i = 0; i < 9; i ++){
char c = array[i]+48;
cout << c << " - ";
}
return 0;
I am making a sudoku board, and it reads a file containing integers,...
I assume you mean a text file, not a binary file. So it actually it contains chars which represent ints which are converted to ints when you read them in using cin > intVar.
If so, you could read your text file as chars instead.
Afterall, the logic to look for what values are in each row, column, etc will be the same if you use '1', '2', '3', as if you use 1, 2, 3, ... Just with a different variable type.
PS Ignoring the spurious ; after the board input parameter (syntax error!) and the missing braces round the function (ditto)
Edit: no doubt stating the obvious!
1 2 3 4 5 6 7 8 9 10 11 12
int subFunction(int board[][9]/*;*/) {for (int i = 0; i < 9; i++)
{
for (int j = 0; int j < 9; j++)
{
if (board[i][j] == 0)
board[i][j] = (char)board[i][j]; // this line does nothing
// converts int of 0 to char of 0, then back again
board[i][j] = ' '; // this sets array elem to 32 (ASCII code of ' ')
}
}
}