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?
#include <iostream>
#include <fstream>
usingnamespace 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);
elseif (option == '?')
interact(sudokuBoard);
elseif (option == 'd' || option == 'D')
display(sudokuBoard);
elseif (option == 's' || option == 'S')
showValues(sudokuBoard);
elseif (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