Read from file: Change int to char

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?
Last edited on
If you want the array to contain character data, why not declare it as type char instead of int?
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

and the zeros would need to become spaces.
Last edited on
For a sudoku board, working with integers is convenient.
While displaying the board, we can print a space if the value is zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <sstream>

const std::size_t BOARD_SZ = 5 ;
using board_t = int[BOARD_SZ][BOARD_SZ] ;

std::istream& read( std::istream& stm, board_t& board )
{
    for( auto& row : board ) for( int& value : row ) stm >> value ;
    return stm ;
}

std::ostream& print( const board_t& board, std::ostream& stm = std::cout )
{
    for( const auto& row : board )
    {
        for( int value : row )
        {
            if( value == 0 ) std::cout << "   " ;
            else std::cout << value << "  " ;
        }
        stm << "\n\n" ;
    }

    return stm ;
}

int main()
{
    board_t board {} ;
    std::istringstream file( "0 0 9 7 0\n 0 1 8 0 4\n 1 0 2 1 7\n 1 2 3 4 5\n 1 0 0 8 0\n" ) ;
    if( read( file, board ) ) print(board) ;
}

http://coliru.stacked-crooked.com/a/fde9015d6d703843
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>

using namespace 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;
1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 
1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -
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.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <sstream> // for pretend file
using namespace std;

const int boardWidth  = 3;
const int boardHeight = 3;

// functions use array reference which just takes right size array
bool readBoardFromStream(char (&board)[boardWidth][boardHeight], istream& is);
void displayBoard(char (&board)[boardWidth][boardHeight]);

void test(const char* testData);

int main() {
    const char testData_1[] =
    "000\n"
    "003\n"
    "201\n";

    const char testData_2[] =
    "000\n"
    "103\n"
    "231\n";

    const char testData_3[] =
    "302\n"
    "123\n"
    "231\n";

    const char testData_4[] =
    "312\n"
    "123\n"
    "231\n";

    // all zeros
    const char testData_5[] =
    "000\n"
    "000\n"
    "000\n";

    // not enough digits on some rows
    const char testData_6[] =
    "0\n"
    "01\n"
    "012\n";

    // too many digits on some rows
    const char testData_7[] =
    "012\n"
    "3456\n"
    "789\n";

    // invalid char (not a digit) on some rows
    const char testData_8[] =
    "012\n"
    "3 5\n"
    "789\n";

    test(testData_1);
    test(testData_2);
    test(testData_3);
    test(testData_4);
    test(testData_5);
    test(testData_6);
    test(testData_7);
    test(testData_8);

    return 0;
}

void test(const char* testData) {
    char board[boardWidth][boardHeight] = {0}; // fill with '\0' chars

    istringstream iss(testData); // use ifstream here, in real life...
    iss >> noskipws; // don't skip the whitespace like usual
    // as we want to check that rows end with a newline after 3 chars
    bool retVal = readBoardFromStream(board, iss);

    if(retVal) {
        displayBoard(board);
    } else {
        cout << "load failed\n";
    }

    cout << '\n';
}

bool readBoardFromStream(char (&board)[boardWidth][boardHeight], istream& is) {
    for (int row = 0; row < boardHeight; ++row) {
        for (int col = 0; col < boardWidth; ++col) {
            char ch = '\0';
            is >> ch;
            if(!isdigit(ch)) {
                cout << "? expected digit!\n";
                return false;
            }
            board[row][col] = (ch == '0') ? '.' : ch;
        }
        {
            // check we get a newline here
            char ch = '\0';
            is >> ch;
            if(ch != '\n') {
                cout << "? expected newline!\n";
                return false;
            }
        }
    }
    return true;
}

void displayBoard(char (&board)[boardWidth][boardHeight]) {
    for (int row = 0; row < boardHeight; ++row) {
        for (int col = 0; col < boardWidth; ++col) {
            cout << ' ' << board[row][col];
        }
        cout << '\n';
    }
}


(Used . rather than space for test purposes)

 . . .
 . . 3
 2 . 1

 . . .
 1 . 3
 2 3 1

 3 . 2
 1 2 3
 2 3 1

 3 1 2
 1 2 3
 2 3 1

 . . .
 . . .
 . . .

? expected digit!
load failed

? expected newline!
load failed

? expected digit!
load failed
Last edited on
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 ' ')
      }
   }
}
Last edited on
Topic archived. No new replies allowed.