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?
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.
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.
#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();
}
#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] = {}; // 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 << "|";
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'; // 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
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