testBed issue

So I am writing a sudoku project, like I was in my previous post, I was able to save the written numbers, but the rewritten code is giving me issues, it's not saving the first number. I am pulling it from myGame.txt which includes the numbers 723400159600302008800010002070654020004207300050931040500070003400103006932000714, the first time through it reads the 7 but not the second or third when it's over written, I was wondering if you guys could offer some help. I couted sudoku[0][0] through the program and it goes from a 7 to a 0.
Here is my output of my couted index.
7>
Exp: >
E
> What are the coordinates of the square: B2
Here B2 has a 0 in it so it can be edited

> What is the value at 'B2': 9
>
> 0>
Exp: >
And here is my code beneath.

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
  #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;
   
   
   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();
   cout << endl << endl;
   cout << "> ";
   }
   else if (option == 'D' || option == 'd')
      display(sudokuBoard);
   
   else if (option == 'S' || option == 's')
      showValue(sudokuBoard);

   else if (option == 'E' || option == 'e')
      editBoard(sudokuBoard);
     
   else if (option == 'Q' || 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";
           getOption(sudokuBoard);
   }
   }
/**********************************************************************
 * function will edit a coordinate of the game board based on what
 * was entered
 ***********************************************************************/
void editBoard(char sudokuBoard[][9])
{
   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
   cout << sudokuBoard[0][0];
   sudokuBoard[letter - 'A'][number - 1] = value + 48;
     cin.ignore();
   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;
}
Topic archived. No new replies allowed.