I'm writing a program that will eventually be a Tic-Tac-Toe game, but as for now, I just want to get the layout done. Right now, all it does is read the game board from a file, display it, and then ask you where you want to save it afterwards. I'm just having a couple problems though.
When reading in a file, the format is as follows. There is exactly one space in between each symbol. The "X" symbolizes that played 'X' has taken the spot, the "O" symbolizes that played 'O' has taken the spot. A period represents an empty space:
X O .
. . .
. X .
In the display function, the game board is then displayed to the screen with player positions X and O which were read from the file, but periods are now left as blank spaces.
Here is my code so far. What is wrong?!
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void readFile(char ticTacBoard[][9]);
void writeFile(char ticTacBoard[][9]);
void display(char ticTacBoard[][9]);
/**********************************************************************
* Main: Basically a delegator. Gets others to do its' dirty work.
***********************************************************************/
int main()
{
//Declare array
char ticTacBoard[9][9];
//Calling functions/pass array
readFile(ticTacBoard);
display(ticTacBoard);
writeFile(ticTacBoard);
return 0;
}
/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char ticTacBoard[][9])
{
cout << " " << ticTacBoard[0][0] << " | " << ticTacBoard[1][0] << " | " << ticTacBoard[2][0] << " " << endl
<< "---+---+---" << endl
<< " " << ticTacBoard[0][1] << " | " << ticTacBoard[1][1] << " | " << ticTacBoard[2][1] << " " << endl
<< "---+---+---" << endl
<< " " << ticTacBoard[0][2] << " | " << ticTacBoard[1][2] << " | " << ticTacBoard[2][2] << " " << endl;
}
/**********************************************************************
* Read a file into memory.
***********************************************************************/
void readFile(char ticTacBoard[][9])
{
//Declare variable/array
char sourceFile[256];
//Declare file-input.
ifstream fin;
//Get filename from user
cout << "Enter source filename: ";
cin >> sourceFile;
//Open file with error checking
fin.open(sourceFile);
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
//Read from file into array
for (int i = 0; i <= 9; i++)
{
fin >> ticTacBoard[i];
}
//Close the file
fin.close();
}
/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char ticTacBoard[][9])
{
//Delcare file-output
ofstream fout;
char destinationFile[256];
//Asking for user input
cout << "Enter destination filename: ";
cin >> destinationFile;
//Open destination file & error checking
fout.open(destinationFile);
if (fout.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
else
cout << "File written";
//Writes board to file
for (int i = 0; i <= 9; i++)
{
fout << ticTacBoard[i-1] << " ";
if (i % 3 == 0)
{
fout << endl;
}
}
//Close file
fout.close();
}