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.
#include <iostream>
#include <fstream>
usingnamespace 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 << "|";
elseif (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();
elseif (option == 'D')
display(sudokuBoard);
elseif (option == 'E')
editBoard(sudokuBoard);
elseif (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();
}
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.
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.
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.
#include <iostream>
#include <fstream>
usingnamespace 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 << "|";
elseif (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();
elseif (option == 'D')
display(sudokuBoard);
elseif (option == 'S')
showValue(sudokuBoard);
elseif (option == 'E')
editBoard(sudokuBoard);
elseif (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;
}
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);
}