save to file error

i took this up from some old code I found on this forum. I fixed a few of the errors but I found 2 errors that I cant seem to get around.

This is a Sudoku game, the game .txt file looks like this.
7 2 3 0 0 0 1 5 9
6 0 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 0 0 7 0 0 0 3
4 0 0 1 0 3 0 0 6
9 3 2 0 0 0 7 1 4


the game just displays this file and we fill in the blanks (0 = " ").
quite a nice program actually.

1. when saving the "input" becomes, well...it doesnt save right.

2. some functions don't loop correctly, like the '?' option just closes the program.

the code compiles just fine.

Also the code didn't fit on one screen so I had to use two comment blocks.

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
241
242
243
244
#include <iostream>
#include <fstream>
using namespace std;

void readFile(char sudokuBoard[][9]);
void writeFile(char sudokuBoard[][9]);
void display(char sudokuBoard[][9]);
void interact();
void getOption(char sudokuBoard[][9]);
void editSquare(char sudokuBoard[][9]);
void showValues();

/**********************************************************************
* Main: Basically a delegator. Just makes other functions do its'
*       dirty work for it.
***********************************************************************/
int main()
{
   //Declare array
   char sudokuBoard[9][9];

   //Calling other functions/pass array
   readFile(sudokuBoard);
   interact();
   display(sudokuBoard);

   return 0;
}


/**********************************************************************
* readFile: Asks the user for a filename, reads a gameboard in from
*           that file name, and then places it in an array.
***********************************************************************/
void readFile(char sudokuBoard[][9])
{
   //Declare filename
   char sourceFile[256];

   //Declare file input
   ifstream fin;

   //Get filename from user
   cout << "Where is your board located? ";
   cin  >> sourceFile;

   //Open file with error checking
   fin.open(sourceFile);
   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read file into array
   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] = ' ';
         }
      }
   }

   //Close the file
   fin.close();
}

/***********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl

   //Row 1
        << "1  "
        << sudokuBoard[0][0]
        << " " << sudokuBoard[1][0]
        << " " << sudokuBoard[2][0]
        << "|"
        << sudokuBoard[3][0]
        << " " << sudokuBoard[4][0]
        << " " << sudokuBoard[5][0]
        << "|"
        << sudokuBoard[6][0]
        << " " << sudokuBoard[7][0]
        << " " << sudokuBoard[8][0]
        << endl

   //Row 2
        << "2  "
        << sudokuBoard[0][1]
        << " " << sudokuBoard[1][1]
        << " " << sudokuBoard[2][1]
        << "|"
        << sudokuBoard[3][1]
        << " " << sudokuBoard[4][1]
        << " " << sudokuBoard[5][1]
        << "|"
        << sudokuBoard[6][1]
        << " " << sudokuBoard[7][1]
        << " " << sudokuBoard[8][1]
        << endl

   //Row 3
        << "3  "
        << sudokuBoard[0][2]
        << " " << sudokuBoard[1][2]
        << " " << sudokuBoard[2][2]
        << "|"
        << sudokuBoard[3][2]
        << " " << sudokuBoard[4][2]
        << " " << sudokuBoard[5][2]
        << "|"
        << sudokuBoard[6][2]
        << " " << sudokuBoard[7][2]
        << " " << sudokuBoard[8][2]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 4
        << "4  "
        << sudokuBoard[0][3]
        << " " << sudokuBoard[1][3]
        << " " << sudokuBoard[2][3]
        << "|"
        << sudokuBoard[3][3]
        << " " << sudokuBoard[4][3]
        << " " << sudokuBoard[5][3]
        << "|"
        << sudokuBoard[6][3]
        << " " << sudokuBoard[7][3]
        << " " << sudokuBoard[8][3]
        << endl

   //Row 5
        << "5  "
        << sudokuBoard[0][4]
        << " " << sudokuBoard[1][4]
        << " " << sudokuBoard[2][4]
        << "|"
        << sudokuBoard[3][4]
        << " " << sudokuBoard[4][4]
        << " " << sudokuBoard[5][4]
        << "|"
        << sudokuBoard[6][4]
        << " " << sudokuBoard[7][4]
        << " " << sudokuBoard[8][4]
        << endl

   //Row 6
        << "6  "
        << sudokuBoard[0][5]
        << " " << sudokuBoard[1][5]
        << " " << sudokuBoard[2][5]
        << "|"
        << sudokuBoard[3][5]
        << " " << sudokuBoard[4][5]
        << " " << sudokuBoard[5][5]
        << "|"
        << sudokuBoard[6][5]
        << " " << sudokuBoard[7][5]
        << " " << sudokuBoard[8][5]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 7
        << "7  "
        << sudokuBoard[0][6]
        << " " << sudokuBoard[1][6]
        << " " << sudokuBoard[2][6]
        << "|"
        << sudokuBoard[3][6]
        << " " << sudokuBoard[4][6]
        << " " << sudokuBoard[5][6]
        << "|"
        << sudokuBoard[6][6]
        << " " << sudokuBoard[7][6]
        << " " << sudokuBoard[8][6]
        << endl

   //Row 8
        << "8  "
        << sudokuBoard[0][7]
        << " " << sudokuBoard[1][7]
        << " " << sudokuBoard[2][7]
        << "|"
        << sudokuBoard[3][7]
        << " " << sudokuBoard[4][7]
        << " " << sudokuBoard[5][7]
        << "|"
        << sudokuBoard[6][7]
        << " " << sudokuBoard[7][7]
        << " " << sudokuBoard[8][7]
        << endl

   //Row 9
        << "9  "
        << sudokuBoard[0][8]
        << " " << sudokuBoard[1][8]
        << " " << sudokuBoard[2][8]
        << "|"
        << sudokuBoard[3][8]
        << " " << sudokuBoard[4][8]
        << " " << sudokuBoard[5][8]
        << "|"
        << sudokuBoard[6][8]
        << " " << sudokuBoard[7][8]
        << " " << sudokuBoard[8][8]
        << "\n"
        << "\n";

   getOption(sudokuBoard);
}


/*************************************************************************
* Interact: Allows the user to interact and manipulate the game board.
*
************************************************************************/
void interact()
{
   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"
        << "\n";

   return;
}
Last edited on
Sorry code was too long to fit on one. Here is the rest!


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
/*************************************************************************
* getOption: Gets the user's input.
*
************************************************************************/
void getOption(char sudokuBoard[][9])
{
   char option;
   cout << "> ";
   cin >> option;

   if (option == 'e' || option == 'E')
      editSquare(sudokuBoard);
   else if (option == '?')
      interact();
   else if (option == 'd' || option == 'D')
      display(sudokuBoard);
   else if (option == 's' || option == 'S')
      showValues();
   else if (option == 'q' || option == 'Q')
      writeFile(sudokuBoard);
   else
      cout << "ERROR: Invalid command";

   return;
}


/***********************************************************************
* editSquare: Edits one square of the table based on the coordinates
*             entered by the user.
************************************************************************/
void editSquare(char sudokuBoard[][9])
{
   //Declare variables
   char letter;
   int number;
   int value = 0;

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

   //Converts letter to uppercase
   letter = toupper(letter);

   //If square is full, display "read only" message
   if (sudokuBoard[letter - 65][number - 1] != ' ')
   {
      cout << "ERROR: Square \'" << letter << number << "\' is read-only\n";
      cout << "\n";
      getOption(sudokuBoard);
   }
   else
   {
      //Gets value to place in specified coordinates
      cout << "What is the value at \'" << letter << number << "\': ";
      cin  >> value;

      //Makes sure value is within correct range
      if (value < 1 || value > 9)
      {
         cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
         cout << "\n";
         getOption(sudokuBoard);
      }

      cout << "\n";
      sudokuBoard[letter - 65][number - 1] = value;
      getOption(sudokuBoard);
   }

   return;
}
/******************************************************************************
* writeFile: Writes the contents of the board to a file to be picked up later.
*
*****************************************************************************/
void writeFile(char sudokuBoard[][9])
{
   //Declare file output
   ofstream fout;
   char destinationFile[256];

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

   //Open destination file & error checking
   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit (1);
   }
   else
      cout << "Board written successfully";

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

         fout << sudokuBoard[row][col];

         //Makes sure it's a 9x9 grid
         if (row % 9 == 0)
         {
            fout << endl;
         }
      }
   }

   //Close file
   fout.close();
}

/**************************************************************************
* showValues: Shows all the possible values for a given set of coordinates.
*
**************************************************************************/
void showValues()
{
   //Declare variables
   char letter;
   int number;

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

   //Converts letter to uppercase
   letter = toupper(letter);

   return;
}
Please show us some examples, for example, when you want to save 9, what do you get instead?

Anyway, did you try this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Makes sure value is within correct range
      if (value < 1 || value > 9)
      {
         cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
         cout << "\n";
         getOption(sudokuBoard);
      }
      
      else
      {
         cout << "\n";
         sudokuBoard[letter - 65][number - 1] = value;
         getOption(sudokuBoard);
      }

Last edited on
it just looks blank when I copy paste. so here is a link to my .txt.

https://dl.dropbox.com/u/3004642/mygames.txt

here is a run i just did, after implementing your changes and others that I found.


Where is your board located? mygame.txt
Options:
   ?  Show these instructions
   D  Display the board
   E  Edit one square
   S  Show the possible values for a square
   Q  Save and Quit

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

> e
What are the coordinates of the square: d1
What is the value at 'D1': 4

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

>
SudokuBoard is char[][9], value is int.
what is it that you mean?
ToniAz was referring to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void editSquare(char sudokuBoard[][9])
{
   //Declare variables
   char letter;
   int number;
   // Change this to char
   int value = 0;

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

   //Converts letter to uppercase
   letter = toupper(letter);

   //If square is full, display "read only" message
   if (sudokuBoard[letter - 65][number - 1] != ' ')
   {
      cout << "ERROR: Square \'" << letter << number << "\' is read-only\n";
      cout << "\n";
      getOption(sudokuBoard);
   }
haha.. I got ya. working on it right now. Any pointers. My code always seems...well, unorganized.
Not quite sure why you decided to go with a character array. I would have stuck with an int array and handle it the same way you read in your file, treat 0s as an empty space. To me, it just makes more sense since the only time you're actually using a char is for the space, but that only requires a simple if statement.

Also, an int array of 9x9 allows to check that the puzzle is done correctly. You can check the values up online for what you check for, but I believe it's 45, I could be, and most likely wrong, for the values across each row and down each column. Also, each 3x3 square should equal that value as well.

Aside from that. I haven't read over your code extensively, but I'm sure another set of eyes will pick out some more improvements.
well this was a semi finished code that I found. Im trying to finish it and from the way it was looking at first, I thought it would be a good challenge.

kinda like, looking at things a new way.
Topic archived. No new replies allowed.