I'm creating a Tic-Tac-Toe game. Well, at least the beginnings of it. Currently, I'm trying to read a game board from a file, display it to the screen, and then write the contents back to a different destination file that the user selects.

My problem is this: when reading the game board from a file, it uses "." to indicate a place in which neither user has made a move. When I display it to the screen, I need those periods to be spaces. But then when time to write it back to the destination file, I need those periods to be back there.

How would one go about doing this? I'm quite lost.

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

void readFile(char ticTacBoard[][3]);
void writeFile(char ticTacBoard[][3]);
void display(char ticTacBoard[][3]);

/**********************************************************************
* Main: Basically a delegator. Gets others to do its' dirty work.
***********************************************************************/
int main()
{
   //Declare array
   char ticTacBoard[3][3];

   //Calling functions/pass array
   readFile(ticTacBoard);
   display(ticTacBoard);
   writeFile(ticTacBoard);

   return 0;
}


/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char ticTacBoard[][3])
{
   cout << " " << ticTacBoard[0][0]  //Row 1
        << " | " << ticTacBoard[1][0]
        << " | " << ticTacBoard[2][0]
        << " " << endl

        << "---+---+---" << endl

        << " " << ticTacBoard[0][1]  //Row 2
        << " | " << ticTacBoard[1][1]
        << " | " << ticTacBoard[2][1]
        << " " << endl

        << "---+---+---" << endl

        << " " << ticTacBoard[0][2]  //Row 3
        << " | " << ticTacBoard[1][2]
        << " | " << ticTacBoard[2][2]
        << " " << endl;
}


/**********************************************************************
* Read a file into memory.
***********************************************************************/
void readFile(char ticTacBoard[][3])
{
   //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 < 3; i++)
   {
      for (int j = 0; j < 3; j++)
      {
         fin >> ticTacBoard[j][i];
      }
   }

   //Close the file
   fin.close();
}


/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char ticTacBoard[][3])
{
   //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 < 3; i++)
   {
      for (int j = 0; j < 3; j++)
      {
         fout << ticTacBoard[j][i] << " ";

         //Makes sure it is a 3x3 grid
         if (j % 3 == 0)
         {
            fout << endl;
         }

      }

   }

   //Close file
   fout.close();
}

Any help is appreciated!

if char = '.' then print ' ' else print char
if char = ' ' then print '.' else print char

something like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.