edit Board

So I am in the middle of a sudoku project, but I am having no success trying to see if the space in the array is filled or not, if it is empty I want to add a number to it, but I keep getting a message it is full no matter if it is filled or not. Can someone look at this function for me?

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
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);
}

if you need to I can post more of my code, but I feel like the error should be here.
its cleaner to just use letters.
letter - 'A' is more understandable, it is the exact same result.

are you by any chance typing 'a' by accident? Should you toupper(letter)?

did you initialize the input array to all spaces?

The code seems ok, here. Its probably one of the above.

I did type the capital letter with a number afterwards, it may be your second question, but I'm pretty sure I did that to. do you mean like put the numbers and spaces in each part of the array? or maybe I don't understand you..
Here is my whole code, to help look at the whole picture. I didn't to the toupper function, but If I type in a capital A2 it should still work. Let me know what you think.
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();
}
I keep getting a message it is full no matter if it is filled or not

There are several issues in your code, but to solve the one you are asking for you can try a solution like the following:
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
#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] = {}; // initialize every element
   
   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++)
      {
         char tmp {};
         fin >> tmp;
         if(tmp == '0') { tmp = ' '; }
         sudokuBoard[col][row] = tmp;
      }
   }
   fin.close();
}
/**********************************************************************
 * displays the suduko board and replaces zeros with null characters.
 ***********************************************************************/
void display(char sudokuBoard[][9])
{
   char option;  // <-- warning: unused variable
   // 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'; // warning: statement has no effect
         }
         
         fout << sudokuBoard[col][row];
      }
   }
   fout.close();
}


output:
Where is your board located? sudoku.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  1 2 3|4 5 6|7 8 9
2  1 2 3|4 5 6|7 8 9
3  1 2 3|4 5 6|7 8 9
   -----+-----+-----
4  1 2 3|4 5 6|7 8 9
5  1   3|4 5 6|7 8 9
6  1 2 3|4 5 6|7 8 9
   -----+-----+-----
7  1 2 3|4 5 6|7 8 9
8  1 2 3|4 5 6|7 8 9
9  1 2 3|4 5 6|7 8 9

> E
What are the coordinates of the square: B 5
What is the value at 'B5': 4


sudoku.txt:
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 0 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9


BTW, using std::array instead of C-style arrays would make your code far easier to develop.
Next time you give a code that reads from a file, please consider providing a sample of the file to be read.
I am getting this response when I make the changes you made.
project12.cpp:59:15: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
char tmp {};
^
project12.cpp:59:20: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
char tmp {};
I think it's a compiler issue, how would you do the same thing with an older compiler for c++ ?
here is the file I am reading from
myGame.txt

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
Last edited on
I fixed it I just changed my if statement to have != '0', and it seemed to be edited! :D
Topic archived. No new replies allowed.