save function

So I am creating a Sudoku board and it displays just fine, but when I save the input to a different file and reopen it back up it gives me craziness. The board should be
7 2 3 0 0 0 1 5 9
6 9 0 3 0 2 0 0 8
8 0 0 0 1 0 0 0 2
0 7 0 6 5 4 0 2 0
0 0 4 2 0 7 3 0 0
0 5 0 9 3 1 0 4 0
5 0 7 0 7 0 0 0 3
4 0 0 1 0 3 0 0 6
9 3 2 0 0 0 7 1 4
and this is what it gives me.
> A B C D E F G H I
> 1 \0
Exp: 1 7 2 3| |1 5 9\n
> 2 3| |1 5 9\n
Exp: 2 6 9 |3 2| 8\n
> 2 6 3| 2 | 8 8\n
Exp: 3 8 | 1 | 2\n
> 3 |1 | 2 \n
Exp: -----+-----+-----\n
> -----+-----+-----\n
Exp: 4 7 |6 5 4| 2 \n
> 4 7 6|5 4 |2 \n
Exp: 5 4|2 7|3 \n
> 5 4 2| 7 3| \n
Exp: 6 5 |9 3 1| 4 \n
> 6 5 9|3 1 |4 5\n
Exp: -----+-----+-----\n
> -----+-----+-----\n
Exp: 7 5 | 7 | 3\n
> 7 |7 | 3 4\n
Exp: 8 4 |1 3| 6\n
> 8 1| 3 | 6 9\n
Exp: 9 9 3 2| |7 1 4\n
> 9 3 2 | 7|1 4 ▒\n

but when I save it and then try to open it again, it's a little hectic. Can someone point what I am doing wrong, it seems to read only a few numbers for some reason (and it doesn't look like there is any pattern to it, just sporadic. Can someone point out what I am doing wrong, I thought I would just use the same for loop that I read it to write it, but apparently that doesn't work.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  #include <iostream>
#include <fstream>
using namespace std;

void readFile(char sudokuBoard[][9]);
void displayOptions();
void display(char sudokuBoard[][9]);
void getOption(char sudokuBoard[][9]);
void editBoard(char sudokuBoard[][9]);
void writeFile(char sudokuBoard[][9]);
/**********************************************************************
 * driver program for the functions below
 ***********************************************************************/
int main()
{
   char sudokuBoard[9][9];
   
   readFile(sudokuBoard);
   displayOptions();
   display(sudokuBoard);
   return 0;
}

/**********************************************************************
 * reads the file back into our board[][] array.
 ***********************************************************************/
void readFile(char sudokuBoard[][9])
{
   char initialFile[256];

   ifstream fin;
   
   cout << "Where is your board located? ";
   cin >> initialFile;

   fin.open(initialFile);
   if (fin.fail())
   {
      cout << "Error reading file";
   }
   for (int row = 0; row < 9; row++)
   {
      for (int col = 0; col < 9; col++)
      {
         fin >> sudokuBoard[col][row];
      }
   }
   fin.close();
}
/**********************************************************************
 * displays the suduko board and replaces zeros with null characters.
 ***********************************************************************/
void display(char sudokuBoard[][9])
{
   char option;
   // display sudoku board row by row
   cout << endl
        << "   A B C D E F G H I\n";
   
      for (int row = 0; row < 9; row++)
      {
         cout << row + 1 << "  ";
         for (int col = 0; col < 9; col++)
         {
            if (sudokuBoard[col][row] == '0')
               cout << " ";
            else 
               cout << sudokuBoard[col][row];
            if (col == 2 || col == 5)
               cout << "|";
            else if (col !=8)
               cout << " ";
         }
         
         if (row == 2 || row == 5)
            cout << "\n   -----+-----+-----\n";
         else
            cout << endl;
      }
      cout << endl;
   getOption(sudokuBoard);
}

/**********************************************************************
 * function displays the options the gamer can choose from.
 ***********************************************************************/
void displayOptions()
{
   
   cout << "Options:\n"
        << "   ?  Show these instructions\n"
        << "   D  Display the board\n"
        << "   E  Edit one square\n"
        << "   S  Show the possible values for a square\n"
        << "   Q  Save and Quit\n";
   
}

/**********************************************************************
 * This function will proceed to go to the option they choose.
 ***********************************************************************/
void getOption(char sudokuBoard[][9])
{
   char option;
   cout << "> ";
   cin >> option;
 
   if (option == '?')
   displayOptions();
   
   else if (option == 'D')
      display(sudokuBoard);

   else if (option == 'E')
      editBoard(sudokuBoard);
     
   else if (option == 'Q')
      writeFile(sudokuBoard);
}

/**********************************************************************
 * function will edit a coordinate of the game board based on what
 * was entered
 ***********************************************************************/
void editBoard(char sudokuBoard[][9])
{
   //let's use the ASCII values for A-I (65-73)
   char letter;
   int number;
   int value;

   
   cout << "What are the coordinates of the square: ";
   cin >> letter >> number;

   if (sudokuBoard[letter - 65][number - 1] != ' ')
   {
      cout << "ERROR: Square \'" << letter << number <<
         "\'" << " is filled\n";
   }
   else
   {
      cout << "What is the value at \'" << letter << number << "\': ";
      cin >> value;
   }
   
   cout << endl; 
   sudokuBoard[letter-65][number-1] = value;
   getOption(sudokuBoard);
}

/**********************************************************************
 * Function will write the file to the file the user chooses
 ***********************************************************************/
void writeFile(char sudokuBoard[][9])
{
   ofstream fout;
   char fileDestination[256];

   cout << "What file would you like to write your board to: ";
   cin >> fileDestination;

   fout.open(fileDestination);
   if(fout.fail())
   {
      cout << "Output unsuccessful.\n";
   }
   else
      cout << "Board written successfully.";

   for (int row = 0; row < 9; row++)
   {
      for (int col = 0; col < 9; col++)
      {
         if (sudokuBoard[col][row] == ' ')
         {
            sudokuBoard[col][row] == '0';
         }
         
         fout << sudokuBoard[col][row];
      }
   }
   fout.close();
}
Last edited on
Hello alextexasfan12,

On line 180 try fout << sudokuBoard[col][row] << " ";. The "fin>>" is similar to "cin>>" each will read until either a white space or "\n" is encountered. Your "fout" is putting all the numbers togather like "123456789" which is not what you want. The output file should look more like this "1 2 3 4 5 6 7 8 9". This way when you read the file it will ony read one number at a time.

Hope that helps,

Andy
I tried this previously and it is still giving me numbers in the wrong places, plus I'm getting a null character at the very beginning and I'm not understanding how it's getting there.
Hello alextexasfan12,

Is your problem solved now? If so how?

While working with your program I found several parts that could be improved or changes.

Andy
This exact problem is solved, but I'm still trying to work out some kinks, it's not saving the edited input from the user for an empty space. But at least it's displaying the saved file in the right format.
As far as your second question I converted the edited integer back to ASCII so it wouldn't display a null character, but now the edited number isn't showing up in the rewritten file.
I have made changes to my code where it doesn't display the null character, but doesn't save the number that is put in. Here is the code beneath. Again, it's a little complicated b/c I used some ASCII conversion that probably could have been avoided.
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <fstream>
using namespace std;

void readFile(char sudokuBoard[][9]);
void displayOptions();
void display(char sudokuBoard[][9]);
void getOption(char sudokuBoard[][9]);
void editBoard(char sudokuBoard[][9]);
void showValue (char sudokuBoard[][9]);
void writeFile(char sudokuBoard[][9]);
/**********************************************************************
 * driver program for the functions below
 ***********************************************************************/
int main()
{
   char sudokuBoard[9][9];
   
   readFile(sudokuBoard);
   displayOptions();
   display(sudokuBoard);
   return 0;
}

/**********************************************************************
 * reads the file back into our board[][] array.
 ***********************************************************************/
void readFile(char sudokuBoard[][9])
{
   char initialFile[256];

   ifstream fin(initialFile);
   
   
   cout << "Where is your board located? ";
   cin >> initialFile;

   fin.open(initialFile);
   if (fin.fail())
   {
      cout << "Error reading file";
   }

   for (int row = 0; row < 9; row++)
   {
      for (int col = 0; col < 9; col++)
      {
         fin >> sudokuBoard[col][row];
      }
   }
   fin.close();
}
/**********************************************************************
 * displays the suduko board and replaces zeros with null characters.
 ***********************************************************************/
void display(char sudokuBoard[][9])
{
   // display sudoku board row by row
   cout << endl
        << "   A B C D E F G H I\n";
   
      for (int row = 0; row < 9; row++)
      {
         cout << row + 1 << "  ";
         for (int col = 0; col < 9; col++)
         {
            if (sudokuBoard[col][row] == '0')
               cout << " ";
            else 
               cout << sudokuBoard[col][row];
            if (col == 2 || col == 5)
               cout << "|";
            else if (col !=8)
               cout << " ";
         }
         
         if (row == 2 || row == 5)
            cout << "\n   -----+-----+-----\n";
         else
            cout << endl;
      }
      cout << endl;
   getOption(sudokuBoard);
}

/**********************************************************************
 * function displays the options the gamer can choose from.
 ***********************************************************************/
void displayOptions()
{
   
   cout << "Options:\n"
        << "   ?  Show these instructions\n"
        << "   D  Display the board\n"
        << "   E  Edit one square\n"
        << "   S  Show the possible values for a square\n"
        << "   Q  Save and Quit\n";
   
}

/**********************************************************************
 * This function will proceed to go to the option they choose.
 ***********************************************************************/
void getOption(char sudokuBoard[][9])
{
   char option;
   cout << "> ";
   cin >> option;
   toupper(option);
 
   if (option == '?')
   displayOptions();
   
   else if (option == 'D')
      display(sudokuBoard);
   
   else if (option == 'S')
      showValue(sudokuBoard);

   else if (option == 'E')
      editBoard(sudokuBoard);
     
   else if (option == 'Q')
      writeFile(sudokuBoard);
}


/**********************************************************************
 * function will show all possible values for the coordinates entered
 ***********************************************************************/
void showValue(char sudokuBoard[][9])
{
   char letter;
   int number;
   
   cout << "What are the coordinates of the square: ";
   cin >> letter >> number;

   toupper(letter);

   if (sudokuBoard[letter-65][number-1] != '0')
   {
      cout << "ERROR: Square \'" << letter << number <<
         "\'" << " is filled\n";
   }

   else
   {
      cout << "The possible values for " << letter << number
           << "are";
   }
   }
/**********************************************************************
 * function will edit a coordinate of the game board based on what
 * was entered
 ***********************************************************************/
void editBoard(char sudokuBoard[][9])
{
   //let's use the ASCII values for A-I (65-73)
   char letter;
   int number;
   int value;

   
   cout << "What are the coordinates of the square: ";
   cin >> letter >> number;

   int row = number - 1;
   char col = letter - 16;
   
   if (sudokuBoard[row][col] != '0')
   {
      cout << "ERROR: Square \'" << letter << number <<
         "\'" << " is filled\n";
   }
   else
   {
      cout << "What is the value at \'" << letter << number << "\': ";
      cin >> value;
      for (col = 0; col < 9; col++)
      {
         if (sudokuBoard[row][col] == value)
         {
            cout << "ERROR: Value \'" << value << "\'" << "is invalid\n";
         }
      }
      for (row = 0; row < 9; row++)
      {
         if (sudokuBoard[row][col] == value)
      {
         cout << "ERROR: Value \'" << value << "\'" << "is invalid\n";
      }
      }
   }
   cout << endl;
   
   //converting int(value) to ASCII
   sudokuBoard[row][col] = 48 + value;
   getOption(sudokuBoard);
   
}

/**********************************************************************
 * Function will write the file to the file the user chooses
 ***********************************************************************/
void writeFile(char sudokuBoard[][9])
{
   //Declare file output
   char fileDestination[256];
   ofstream fout;

   //Asking for user input
   cout << "What file would you like to write your board to: ";
   cin  >> fileDestination;

   //Open destination file & error checking
   fout.open(fileDestination);
   if (fout.fail())
   {
      cout << "Written unsuccessfully\n";
   }
   else
      cout << "Board written successfully\n";

   //Writes board to file
   for (int row = 0; row < 9; row++)
   {
     for (int col = 0; col < 9; col++)
      {
         fout << sudokuBoard[col][row];
      }
     
   }
   

   //Close file
   fout.close();
   return;
}
So I edited my save function to save the input but it's not reading the first integer in the array now, from the saved file.
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
void editBoard(char sudokuBoard[][9])
{
   //let's use the ASCII values for A-I (65-73)
   char letter;
   int number;
   int value;

   
   cout << "What are the coordinates of the square: ";
   cin >> letter >> number;
   
   letter = toupper(letter);
   
   if (sudokuBoard[letter - 65][number - 1] != '0')
   {
      cout << "ERROR: Square \'" << letter << number <<
         "\'" << " is filled\n";
   }
   else
   {
      cout << "What is the value at \'" << letter << number << "\': ";
      cin >> value;
     
   }
   cout << endl;
   
   //converting int(value) to ASCII
   sudokuBoard[letter - 65][number - 1] = 48 + value;
   getOption(sudokuBoard);
   
}
What's weird about this, this actually works, but then I do the computer generated test and gets rid of the the first number of the re-written file.
Topic archived. No new replies allowed.