save input to file ??

i've been up way to late. I'm trying to figure out how to take an impute given and add it to my "board" this sudoko game asks the user where the Cordinants are and then asks what to replace it with...now I've got that far, but I cant get it to save the inpute to my file. any ideas on how to do that with the current setup that I have?

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <fstream>
using namespace std;

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

/**********************************************************************
* 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(sudokuBoard);
   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;
   
   for (int y = 0; y<9; y++)
   {
       cout << y + 1 << "  ";
   if ((y % 3) == 0 && y > 0)
       cout<<"-----+-----+-----\n";
   for (int x = 0; x<9; x++)
       {   
       if ((x % 9) != 0 && (x % 9) % 3 == 0)
        cout << "|";
       if (x%3)
           cout<<" ";
   cout << sudokuBoard[x][y];
       }
  cout << endl;
}
   getOption(sudokuBoard);
}


/*************************************************************************
* Interact: Allows the user to interact and manipulate the game board.
*
************************************************************************/
void interact(char sudokuBoard[][9])
{
   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";
        display(sudokuBoard);
   return;
}


/*************************************************************************
* 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(sudokuBoard);
   else if (option == 'd' || option == 'D')
      display(sudokuBoard);
   else if (option == 's' || option == 'S')
      showValues(sudokuBoard);
   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);
      }
      
      else
      {     
      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(char sudokuBoard[][9])
{
   //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);
   getOption(sudokuBoard);
   
   return;
}


void editSquare(char sudokuBoard[][9]) is where the problem is. Line 151
You have to change it to char sudokuBoard[][9][9] , I think. (see the arrays documentation on this site)
ya, not sure how thats supposed to mix with my code.
this will be the death of me. anyone have any ideas on how to save any changes that I want to make to a specific cell?
your question is not clear to me.
But about problem in writefile code

1)
1
2
3
4
if (row % 9 == 0)
         {
            fout << endl;
         }

Use this in first loop after end of second one.

2)option says save and quit but its not quitting there.

Its good program and working if you are starter but you can still improve it.
my question is this.

How do I save the changes that I make? for instance,

>E
>What are the coordinates of the square:
>d1
>What is the Value at 'd1':
>4

those are the steps that I use to edit a square, but when I display the board, there square that I just edited has a Diamond in its place.

I dont know how to fix that.
after changing value just call the function to write in a file.

or edit your writefile function to save file at default location.

You try this but if you fail I will paste the code here.
Topic archived. No new replies allowed.