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.
#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;
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();
cout << endl << endl;
cout << "> ";
}
elseif (option == 'D' || option == 'd')
display(sudokuBoard);
elseif (option == 'S' || option == 's')
showValue(sudokuBoard);
elseif (option == 'E' || option == 'e')
editBoard(sudokuBoard);
elseif (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;
}